将游标数据放入数组中

时间:2013-09-18 04:24:46

标签: android android-sqlite android-cursor

在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

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

5 个答案:

答案 0 :(得分:13)

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

“i”不是游标行索引,而是列索引。游标已定位到特定行。如果需要重新定位光标。使用cursor.move或moveToXXXX(see documentation)。

对于getString / Int / Long等,您只需告诉光标您想要的列。如果您不知道columnIndex,则可以使用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()]);
}

答案 1 :(得分:5)

我希望它对你有用。

 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;

        }

答案 2 :(得分:3)

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

答案 3 :(得分:2)

使用它:

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();

答案 4 :(得分:1)

尝试将数据放入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;
  }