Bogdan Daniel
Bogdan Daniel

Reputation: 2749

How to put this cursor data in ArrayLists - AndroidStudio

I have a cursorLoader which returns a cursor with this data:

    0 {
    [email protected]
    about=Party
    }
    1 {
    [email protected]
    about=Paaarty
    }
    2 {
    [email protected]
    about=activity 2
    }
    3 {
    [email protected]
    about=activity 3
    }
    4 {
    [email protected]
    about=activity 5
    }

How can I save the emails in an ArrayList called emails and the about in an ArrayList called about. I've been trying different things with the cursor but most of the time I just get outOfBounds.

Edit: This is the line that prints it like that:

Log.v("Cursor Object", DatabaseUtils.dumpCursorToString(cursor));

Upvotes: 0

Views: 563

Answers (1)

Andy
Andy

Reputation: 518

If you want to do it your way maybe this method would help (provided that you supply two empty ArrayList) ?

private void populateLists(Cursor cursor, List<String> emails,
        List<String> about) throws IllegalArgumentException {

    if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        do {
            emails.add(cursor.getString(cursor
                    .getColumnIndexOrThrow("email")));
            about.add(cursor.getString(cursor
                    .getColumnIndexOrThrow("about")));
        } while (cursor.moveToNext());
    }
}

Upvotes: 1

Related Questions