4

I have a query which populates the cursor from the database tables rules, in android sqlite. when I use the getcount on my cursor it shows me 24 which means there are 24 rows in the cursor. what I want is that all the data within cursor should be copied to multidimensional array or arraylist or in other words a copy of the database table into array list.

c = obj_db.rawQuery("select * from rules", null);
int count=c.getCount();
String x= String.valueOf(count);

I want the replica of data as in table in array list in short

General Grievance
  • 4,555
  • 31
  • 31
  • 45
ijaz khattak
  • 75
  • 1
  • 1
  • 11

4 Answers4

11

Do like this

ArrayList<String> mArrayList = new ArrayList<String>();
c.moveToFirst();
while(!c.isAfterLast()) {
     mArrayList.add(c.getString(c.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
     c.moveToNext();
}

The important line here is

mArrayList.add(c.getString(c.getColumnIndex(KEY_NAME))); 

Here I am getting the string from the cursor which is placed at column named KEY_NAME, you can getInt etc depending on your data. Just use the corresponding column names.

Edit.

This is an example. You can create a custom List of row type and add data to it like this.

if (cursor != null && cursor.moveToFirst()) {
            //get columns
            int titleColumn = cursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.TITLE);
            int idColumn = cursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media._ID);
            int artistColumn = cursor.getColumnIndex
                    (android.provider.MediaStore.Audio.Media.ARTIST);
            int albumID = cursor.getColumnIndex
                    (MediaStore.Audio.Media.ALBUM_ID);
            int albumColumn = cursor.getColumnIndex
                    (MediaStore.Audio.Media.ALBUM);
            //add row to list
            do {
                long thisId = cursor.getLong(idColumn);
                String thisTitle = cursor.getString(titleColumn);
                String thisArtist = cursor.getString(artistColumn);
                String thisAlbum = cursor.getString(albumColumn);
                String thisAlbumID = cursor.getString(albumID)
                if (thisAlarm + thisNoti + thisRing==0)
                    arrayList.add(new Row(thisId, thisTitle, thisArtist, thisAlbum, thisAlbumID));
            }
            while (cursor.moveToNext());
            cursor.close();
        }
varunkr
  • 5,364
  • 11
  • 50
  • 99
  • will it copy every row into the array list or only a field from a specified column? when i use a method similar to it all i can get is a single field of a column. – ijaz khattak May 05 '16 at 13:06
  • I don't think you can simply copy the whole list without getting each row first. I have added an example. It is very easy to use – varunkr May 05 '16 at 13:13
  • i will update you on this method tomorrow but as i said to @Dinesh Bob, this may be a lot of code when we have more columns. just like we have clone for cursor and array list there should be any bridge between cursor and arraylist – ijaz khattak May 05 '16 at 13:58
  • Thank you @varunkr any how i solved it using som,ewhat realted logic using 2d array but ur comments were very useful for that solution – ijaz khattak May 07 '16 at 04:49
  • @ijazkhattak glad to know it helped, thnx for accepting !! – varunkr May 07 '16 at 11:55
8

You have to create a Java model class. It should have fields for all columns of your table.

Then iterate through the cursor row by row. For each row, get all the columns from the cursor and create your model object. Then add that object to the ArrayList.

Java model class:

public class MyData {
    String column1;
    String column2;

    public MyData(String column1, String column2){
        this.column1 = column1;
        this.column2 = column2;
    }
}

iterating the cursor and adding to the list:

List<MyData> list = new ArrayList<>();

while(cursor.moveToNext()) {
    String column1 = cursor.getString(cursor.getColumnIndex(COLUMN_NAME1));
    String column2 = cursor.getString(cursor.getColumnIndex(COLUMN_NAME2));
    MyData data = new MyData(column1, column2);
    list.add(data);
}

Update:

If you dont want to have a Java model class, you can use List of Lists.

List<List> outerList = new ArrayList<>();
List<Object> innerList = new ArrayList<>();

    while(cursor.moveToNext()) {
        String column1 = cursor.getString(cursor.getColumnIndex(COLUMN_NAME1));
        String column2 = cursor.getString(cursor.getColumnIndex(COLUMN_NAME2));
        innerList.add(column1);
        innerList.add(column2);
        outerList.add(innerList);
    }

However, I would recommend to use the first method which utilizes the Java model class.

Bob
  • 13,447
  • 7
  • 35
  • 45
  • Thanks for your time, but it is good option when we have limited columns, but i might have more then 10 columns and thing might get into spaghetti code. i thought there might be some way where a cursor might be visited row by row. or via a for loop like [r1,c1] to array[0,0] and so on. – ijaz khattak May 05 '16 at 13:54
  • @ijazkhattak Do you even know what `spaghetti code` refers to? It seems you're using random buzz-words... – Phantômaxx May 05 '16 at 14:00
  • 1
    in my opinion, this type of code will be more readable and easy for future maintenance. – Bob May 05 '16 at 14:01
  • Using Java model class will be easy for future maintenance compared to using general array or list. – Bob May 05 '16 at 14:10
  • @ijazkhattak i updated the answer for your use case. In Android, using List of Lists has more performance benefits than a 2 dimensional List. – Bob May 05 '16 at 14:47
  • Thank you @DineshBob any how i solved it using 2d array instead of list or arraylist but your comments lead me to that solution. – ijaz khattak May 07 '16 at 04:47
1

You need to loop into the cursor.

Something like

while (c.moveToNext()) {
    // The instruction to read the data into the cursor
    // int a = c.getString(0);
}
spassador
  • 393
  • 1
  • 12
0

There are multiple ways to iterate cursor.

If you would like to display data try SimpleCursorAdapter.

As for multidimensional array you can modify SimpleCursorAdapter, just get idea in the source code.

Community
  • 1
  • 1
Maxim G
  • 1,479
  • 1
  • 15
  • 23