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
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();
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With