将游标数据放入数组中



作为Android的新手,我在处理以下问题时遇到了麻烦:

public String[] getContacts(){
    Cursor cursor = getReadableDatabase().rawQuery("SELECT name FROM contacts", null);
    String [] names = {""};
    for(int i = 0; i < cursor.getCount(); i ++){
            names[i] = cursor.getString(i);
    }
    cursor.close();
    return names;
}
以下

给了我以下错误:

09-18 10:07:38.616: E/AndroidRuntime(28165): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sqllitetrial/com.example.sqllitetrial.InsideDB}: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 5

我正在尝试将光标内的数据提取到数组中。有人可以帮助我实施吗?

names.add(cursor.getString(i));

"i"不是游标行索引,而是列索引。游标已定位到特定行。如果您需要重新定位光标。使用 cursor.move 或 moveToXXXX(请参阅文档)。

对于getString/Int/Long等,你只需要告诉光标你想要哪一列。如果您不知道列索引,可以使用cursor.getColumnIndex("yourColumnName")

您的循环应如下所示:

public String[] getContacts(){
    Cursor cursor = getReadableDatabase().rawQuery("SELECT name FROM contacts", null);
    cursor.moveToFirst();
    ArrayList<String> names = new ArrayList<String>();
    while(!cursor.isAfterLast()) {
        names.add(cursor.getString(cursor.getColumnIndex("name")));
        cursor.moveToNext();
    }
    cursor.close();
    return names.toArray(new String[names.size()]);
}

我希望它对你有用。

 public static ArrayList<ModelAgents> SelectAll(DbHelper dbaConnection) {
            ArrayList<ModelAgents> Agents_aList = new ArrayList<ModelAgents>();
            SQLiteDatabase sqldb = dbaConnection.openDataBase();
            Cursor cursor = sqldb.rawQuery("SELECT * FROM Agents", null);
            if (cursor != null)// If Cursordepot is null then do
                                // nothing
            {
                if (cursor.moveToFirst()) {

                    do {
                        // Set agents information in model.
                        ModelAgents Agents = new ModelAgents();
                        Agents.setID(cursor.getInt(cursor
                                .getColumnIndex(TblAgents.ID)));
                        Agents.setCode(cursor.getString(cursor
                                .getColumnIndex(TblAgents.CODE)));
                        Agents.setName(cursor.getString(cursor
                                .getColumnIndex(TblAgents.NAME)));
                        Agents_aList.add(Agents);
                    } while (cursor.moveToNext());
                }
                cursor.close();
            }
            sqldb.close();
            return Agents_aList;
        }

当从数据库查询返回游标时,它被放置在游标第一个条目上方的索引 -1 处,因此,在使用游标获取数据之前,已将其移动到其第一个位置。为此添加 cursor.MoveToFirst();后 Cursor cursor = getReadableDatabase().rawQuery("SELECT name FROM contacts", null);

使用这个:

if (cursor != null && cursor.getCount()>0){
  cursor.moveToFirst();
  do{
    for(int i = 0; i < cursor.getCount(); i ++){
      names.add(cursor.getString(i));
    }
  }while(cursor.moveToNext());
}
cursor.close();
尝试将数据

放入ArrayList<String>中,如下所示:

   public ArrayList<String> getContacts(){
        Cursor cursor = getReadableDatabase().rawQuery("SELECT name FROM contacts", null);
        ArrayList<String> names=new ArrayList<String>();
        if (cursor != null)
        {
            if (cursor.moveToFirst()) {
                for(int i = 0; i < cursor.getCount(); i ++){
                      names.add(cursor.getString(i));
                   }
               }
       cursor.close();
     }
     return names;
  }

最新更新