Putting cursor data into an array

后端 未结 5 426
南方客
南方客 2021-02-04 20:57

Being new in Android, I am having trouble dealing with the following:

public String[] getContacts(){
    Cursor cursor = getReadableDatabase().rawQuery(\"SELECT          


        
相关标签:
5条回答
  • 2021-02-04 21:00

    use this:

    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();
    
    0 讨论(0)
  • 2021-02-04 21:03
    names.add(cursor.getString(i));
    

    "i" is not the cursor row index, it's the column index. A cursor is already positioned to a specific row. If you need to reposition your cursor. Use cursor.move or moveToXXXX (see documentation).

    For getString/Int/Long etc. you just need to tell the cursor which column you want. If you don't know the columnIndex you can use cursor.getColumnIndex("yourColumnName").

    Your loop should look like this:

    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()]);
    }
    
    0 讨论(0)
  • 2021-02-04 21:03

    When cursor is returned from a database query it is placed at index -1 that is above the first entry of the cursor so, before using the cursor to get data you have move it to its first position. For that add cursor.MoveToFirst(); after Cursor cursor = getReadableDatabase().rawQuery("SELECT name FROM contacts", null);

    0 讨论(0)
  • 2021-02-04 21:16

    I hope its useful to you.

     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;
    
            }
    
    0 讨论(0)
  • 2021-02-04 21:26

    Try to put the data into the ArrayList<String> as below:

       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;
      }
    
    0 讨论(0)
提交回复
热议问题