Can someone please explain Cursor in android?
Can some one explain how the cursor exactly works ? Or the flow of the following part of the code ? I know that this is sub activity and all but I did not understand how Cursor works exactly.
final Uri data = Uri.parse("content://co开发者_开发问答ntacts/people/");
final Cursor c = managedQuery(data, null, null, null, null);
String[] from = new String[] { People.NAME };
int[] to = new int[] { R.id.itemTextView };
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,R.layout.listitemlayout, c, from, to);
ListView lv = (ListView) findViewById(R.id.contactListView);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
c.moveToPosition(pos);
int rowId = c.getInt(c.getColumnIndexOrThrow("_id"));
Uri outURI = Uri.parse(data.toString() + rowId);
Intent outData = new Intent();
outData.setData(outURI);
setResult(Activity.RESULT_OK, outData);
finish();
}
});
Thanks.
Cursor is like a list/pointer created from a database resource. (In PHP think like a $res from the mysql_query())
When you run
managedQuery(data, null, null, null, null);
You query contacts, it returns a Cursor that is a pointer for the records in the results
Then you create an adapter from this Cursor. The adapter is an object level representation of the results taken from the source, this time is the cursor, aka the records from database. (In PHP for adapter think like an array for Smarty Templates, the array is the adapter)
The setOnItemClickListener should be easy to understand if you know event based programming.
精彩评论