Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 670
Dennie Avatar asked Aug 30 '09 14:08

Dennie


People also ask

What is the use of cursor explain with example in Android?

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.

How does cursor work in Android?

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.

What is Array List Android?

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.


2 Answers

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.)

like image 154
Isaac Waller Avatar answered Oct 03 '22 23:10

Isaac Waller


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(); } 
like image 36
imbrizi Avatar answered Oct 03 '22 23:10

imbrizi