36

How can I create a list Array (the list display First Alphabet when scroll) with the cursor data?

skydroid
  • 703
  • 6
  • 16
Dennie
  • 2,561
  • 13
  • 40
  • 40

5 Answers5

69

Go through every element in the Cursor, and add them one by one to the ArrayList.

ArrayList<WhateverTypeYouWant> mArrayList = new ArrayList<WhateverTypeYouWant>();
for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
    // The Cursor is now set to the right position
    mArrayList.add(mCursor.getWhateverTypeYouWant(WHATEVER_COLUMN_INDEX_YOU_WANT));
}

(replace WhateverTypeYouWant with whatever type you want to make a ArrayList of, and WHATEVER_COLUMN_INDEX_YOU_WANT with the column index of the value you want to get from the cursor.)

Will Richardson
  • 7,012
  • 6
  • 39
  • 50
Isaac Waller
  • 32,041
  • 27
  • 93
  • 107
  • Hi, The important thing I want to do is display "Fist character" in my List. After I get all data from cursor, I don't know how to convert it to an String[] Array to get "First character". It look something like this char firstLetter = mString[firstVisibleItem].charAt(0); – Dennie Sep 01 '09 at 03:55
  • 9
    Friends: the for loop above is not completly right (condition and increment anre swapped). Use `for(mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {}` – Javier Sedano Feb 03 '12 at 09:23
  • 2
    This type of loop is kind of awful to read. The solution from @PearsonArtPhoto is way more elegant and easier to read. – WarrenFaith Jan 24 '14 at 16:07
  • What do you mean by **getWhateverTypeYouWant**? Is that a real method. – IgorGanapolsky Dec 28 '16 at 21:12
41

One quick correction: the for loop above skips the first element of the cursor. To include the first element, use this:

ArrayList<String> mArrayList = new ArrayList<String>();
mCursor.moveToFirst();
while(!mCursor.isAfterLast()) {
     mArrayList.add(mCursor.getString(mCursor.getColumnIndex(dbAdapter.KEY_NAME))); //add the item
     mCursor.moveToNext();
}
jjLin
  • 3,163
  • 8
  • 29
  • 54
imbrizi
  • 3,606
  • 1
  • 20
  • 23
22

Even better than @imbrizi's answer is this:

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

moveToNext() will return false if there isn't anything left, so this reduces the SLOC by a few, and is easier to see.

Even better is to get the column index outside of the loop.

ArrayList<String> mArrayList = new ArrayList<String>();
int columnIndex=mCursor.getColumnIndex(dbAdapter.KEY_NAME)
while(mCursor.moveToNext()) {
     mArrayList.add(mCursor.getString(columnIndex)); //add the item
}
PearsonArtPhoto
  • 35,989
  • 16
  • 107
  • 136
  • 2
    The only correct way to iterate through the Cursor. Other two answers are just....wrong :| – iTwenty Feb 06 '15 at 10:14
  • 1
    Except in weird scenarios, you can usually externalize the getColumnIndex outside the loop – rds Mar 09 '15 at 17:45
3

This one worked really well for me because I wanted an arraylist of objects:

List<MyObject> myList = new ArrayList<String>();
Cursor c = ...

while(c.moveToNext()) {
    myList.add(new MyObject(cursor.getInt(cursor.getColumnIndex("_id")), cursor.getString(cursor.getColumnIndex("column1")), cursor.getInt(cursor.getColumnIndex("column2")));        
}
c.close();

Just make a POJO MyObject and make sure it has a constructor.

Micro
  • 9,083
  • 8
  • 68
  • 104
2

In Kotlin you can use this extension:

fun <T> Cursor.toList(block: (Cursor) -> T) : List<T> {
    return mutableListOf<T>().also { list ->
        if (moveToFirst()) {
            do {
                list.add(block.invoke(this))
            } while (moveToNext())
        }
    }
}

and use it:

val listOfIds = cursor.toList { 
    // create item from cursor. For example get id:
    it.getLong(it.getColumnIndex("_id"))
}
Rafols
  • 1,169
  • 13
  • 10