Android 如何从游标类中检索数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2810615/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 07:29:59  来源:igfitidea点击:

How to retrieve data from cursor class

android

提问by Rakesh

I need to know how to retrieve data from cursor. I need this because the ringtonemanager returns all the audio files in form of cursor object, I need to know how to retrieve the values.

我需要知道如何从游标中检索数据。我需要这个,因为铃声管理器以光标对象的形式返回所有音频文件,我需要知道如何检索这些值。

Anbudan.

安不丹。

回答by Salvador

Once you have the Cursor object, you can do something like this:

拥有 Cursor 对象后,您可以执行以下操作:

if (cursor.moveToFirst()){
   do{
      String data = cursor.getString(cursor.getColumnIndex("data"));
      // do what ever you want here
   }while(cursor.moveToNext());
}
cursor.close();

回答by tcb

This looks a bit better:

这看起来好一点:

for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
    ...
}

回答by Some Noob Student

Salvador's answer will continue to fetch data from the row after the last row because moveToNext()will only return false when the cursor is pointing at the row after the last row. It will continue to iterate even if the cursor is pointing at the last row.

Salvador 的答案将继续从最后一行之后的行中获取数据,因为moveToNext()只有在光标指向最后一行之后的行时才会返回 false。即使光标指向最后一行,它也会继续迭代。

The correct template should be:

正确的模板应该是:

if (cursor.moveToFirst()){
   while(!cursor.isAfterLast()){
      String data = cursor.getString(cursor.getColumnIndex("data"));
      // do what ever you want here
      cursor.moveToNext();
   }
}
cursor.close();