如何从游标android中检索单行数据?

时间:2015-06-11 13:03:23

标签: android

我想按下按钮时从光标检索数据。我第一次要显示第一行数据,按下按钮后我需要显示第二行数据。我怎样才能做到这一点?我的数据库中有更多行。

当我使用以下代码时,我得到最后一行数据。

for(int i=0;i<cursor.getColumnCount();i++){
    String path=c.getString(2);
    Bitmap bitmap=BitmapFactory.decodeFile(path);
    title.setText(c.getString(1));
    brewerimage.setImageBitmap(bitmap);
}

8 个答案:

答案 0 :(得分:1)

您忘了将光标移动到第一行。请试试这个:

if (cursor.moveToFirst())
{
    do
        {
         String path=cursor.getString(2);
         Bitmap bitmap=BitmapFactory.decodeFile(path);
         title.setText(cursor.getString(1));
         brewerimage.setImageBitmap(bitmap);

        } while (cursor.moveToNext());
}

答案 1 :(得分:1)

您可以像这样获取第一行数据 替换为0     cursor.getString(0); 与你的价值;

if (cursor.moveToFirst())
{
    do
        {
         String path=cursor.getString(0);
         title.setText(path);
        } while (cursor.moveToNext());
}

答案 2 :(得分:1)

第一次按钮点击, cursor.moveToFirst();

然后继续使用, cursor.moveToNext()

直到行结束。

更具体一点:

 if (cursor.moveToFirst()) {
    do {
                            String path=c.getString(2);
                            Bitmap bitmap=BitmapFactory.decodeFile(path);
                            title.setText(c.getString(1));
                            brewerimage.setImageBitmap(bitmap);

                        } while(cursor.moveToNext();
}

答案 3 :(得分:1)

试试这个希望它会对你有所帮助:

try {
 if (cursorOne.moveToFirst()) { 

                    String path=c.getString(2);
                    Bitmap bitmap=BitmapFactory.decodeFile(path);
                    title.setText(c.getString(1));
                    brewerimage.setImageBitmap(bitmap);

                //no need to look up column index since you already know it due to the projection used
  }
} finally {
 cursorOne.close();
}

答案 4 :(得分:1)

试试这个..

if (cursor.getCount() > 0) {

                    for(c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {    

                        String path=c.getString(2);
                        Bitmap bitmap=BitmapFactory.decodeFile(path);
                        title.setText(c.getString(1));
                        brewerimage.setImageBitmap(bitmap);

                    }

            }

答案 5 :(得分:1)

阅读完你的评论后,我有一个想法。这就是现在的查询。

第1步

将数据保存在静态ArrayList或POJO类

第2步:

每当您想要点击按钮时,只需增加续+1即可保存。将此计数用作位置并轻松获取值。

答案 6 :(得分:0)

你可以在给定的位置获得特别的感觉。

 public void getRowAt(int posAt){

        SQLiteDatabase db = getReadableDatabase();
        Cursor cursor = db.rawQuery("select * from "+tableName+" LIMIT 1 OFFSET "+posAt, null);

        int count = cursor.getCount();

        while (cursor.moveToNext()){
            // here your can get your row data.
        }
        cursor.close();
        db.close();
    }

答案 7 :(得分:0)

你是否正在使用数据库,所以你从数据库中获取记录我是对的吗? 因此,您在光标中获取记录,以便从您获取数据的列中,您必须在光标中传递该列名,以便获得结果。 例如:

if (cursor.getCount() > 0) {

            cursor.moveToFirst();
            do {
                myDatarecordModel = new DataRecordModel();
                myDatarecordModel.setStrDataRecordingID(cursor.getString(cursor.getColumnIndex(AppStackFields.DATARECORDING_ID)));

 } while (cursor.moveToNext());
        }
        cursor.close();