Tuesday, 17 September 2013

Putting cursor data into an array

Putting cursor data into an array

Being new in Android, I am having trouble dealing with the following:
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;
}
The following gives me the following error:
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
I am trying to extract the data inside the cursor to an array. Can someone
help me with the implementation.

No comments:

Post a Comment