Search code examples
androidcursorsimplecursoradapterandroid-context

Working of a simplecursoradapter


Cursor c = managedQuery(People.CONTENT_URI,null,null,null,People.NAME);
String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};
SimpleCursorAdapter sca = new SimpleCursorAdapter(this,android.R.layout.simple_list_item_1,c,cols,views);
listview.setAdapter(adapter);

I'm using this code snippet to connect my ListView with Cursor.

I want to ask what

String[] cols = new String[]{People.NAME};
int[] views = new int[]{android.R.id.text1};

exactly does ??

and please explain about the arguments required for the constructor of SimpleCursorAdapter


Solution

  • It is a map, telling the adapter which columns from your cursor to use to fill which Widgets in your layout.

    They get used in the order given. The data in the first column listed in the from array ( you called it cols ) will go into the first id listed in the to array ( you called it views), and so on.

    The other parameters are the layout containing the view ids your specify in the to array and the cursor containing the data to be used in the array.