How can I create a list Array (the list display First Alphabet when scroll) with the cursor data?
Introduction to Cursor in Android The basic purpose of a cursor is to point to a single row of the result fetched by the query. We load the row pointed by the cursor object. By using cursor we can save lot of ram and memory. Here, we pass the table name, column name only then we receive the cursor.
Cursors are what contain the result set of a query made against a database in Android. The Cursor class has an API that allows an app to read (in a type-safe manner) the columns that were returned from the query as well as iterate over the rows of the result set.
ArrayList is a dynamic data structure in which you can add or remove any number of elements and those elements are stored in ordered sequence. It may also contain duplicate values. ArrayList are the implementations of List interface. The package you required to import the ArrayList is import java.
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.)
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(); }
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