How can I create a list Array with the cursor data in Android

前端 未结 5 1539
遥遥无期
遥遥无期 2020-12-04 12:18

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

5条回答
  •  臣服心动
    2020-12-04 13:07

    Even better than @imbrizi's answer is this:

    ArrayList mArrayList = new ArrayList();
    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 mArrayList = new ArrayList();
    int columnIndex=mCursor.getColumnIndex(dbAdapter.KEY_NAME)
    while(mCursor.moveToNext()) {
         mArrayList.add(mCursor.getString(columnIndex)); //add the item
    }
    

提交回复
热议问题