+-
android cursor.moveToNext()?
我试图将表中的所有列查询为一个长文本视图和/或字符串.我知道这可能不是正确的做事方式,但我必须这样做.如果我错了,请纠正我,我的印象是,接下来的行将获得行中的下一列:

Cursor c = db.get();
if(c.moveToFirst){
do{
string = c.getString(0);
}while(c.moveToNext);

我认为这将获得第一列并显示其所有内容,而不是我得到第一列和第一行.我究竟做错了什么?有没有更好或真实的方法来获取这些信息而不使用ListView?

最佳答案
为清楚起见,我认为有兴趣的完整例子如下.正如代码注释所示,我们基本上遍历数据库行,然后是列,以根据数据库形成数据表.

    Cursor cursor = getActivity().getContentResolver().query(uri, projection, null, null,
            null);

    //if the cursor isnt null we will essentially iterate over rows and then columns
    //to form a table of data as per database.
    if (cursor != null) {

        //more to the first row
        cursor.moveToFirst();

        //iterate over rows
        for (int i = 0; i < cursor.getCount(); i++) {

            //iterate over the columns
            for(int j = 0; j < cursor.getColumnNames().length; j++){ 

                //append the column value to the string builder and delimit by a pipe symbol
                stringBuilder.append(cursor.getString(j) + "|"); 
            }
            //add a new line carriage return
            stringBuilder.append("\n");

            //move to the next row
            cursor.moveToNext();
        }
        //close the cursor
        cursor.close();
    }
点击查看更多相关文章

转载注明原文:android cursor.moveToNext()? - 乐贴网