
Explain (and correct) what happens in this code:
Code:
Parental Activity.
public class ContactPickerTester extends Activity { public static int PICK_CONTACT = 1; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.contentpickertester); Button button = (Button) findViewById(R.id.pick_contact_button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View _view) { Intent intent = new Intent(Intent.ACTION_PICK, Uri.parse("content://contacts/")); startActivityForResult(intent, PICK_CONTACT); } }); } public void onActivityResult(int reqCode, int resCode, Intent data) { switch (reqCode) { case (PICK_CONTACT): { if (resCode == Activity.RESULT_OK) { Uri contactData = data.getData(); Cursor c = getContentResolver().query(contactData, null, null, null, null); c.moveToFirst(); String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME_PRIMARY)); c.close(); TextView tv = (TextView) findViewById(R.id.selected_contact_textview); tv.setText(name); } break; } default: break; } } } Child Activity
public class ContactPicker extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_contact_picker); final Cursor c = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); String[] from = new String[] {ContactsContract.Contacts.DISPLAY_NAME_PRIMARY}; int[] to = new int[] {R.id.itemTextView}; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.listitemlayout, c, from, to, 0); ListView lv = (ListView) findViewById(R.id.contactListView); lv.setAdapter(adapter); lv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int pos, long id) { c.moveToPosition(pos); int rowId = c.getInt(c.getColumnIndexOrThrow("_id")); Uri outUri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, rowId); Intent outData = new Intent(); outData.setData(outUri); setResult(Activity.RESULT_OK, outData); } }); } } (3) we get a Cursor object which contains a table from the content of the provider containing the contacts, and ContactsContact.Contacts.CONTENT_URI is a link to the table in the ContentProvider.
(4) Get the value of the "_id" column by its index. So?
(5) This is what and why? As I understand it, we take the path to the table in the ContentProvider and add a further path to the "_id" column. So? Next, we pack into the Intent and send the parent Activity.
(1) After receiving data from the incoming Intent using the query method, it already queries Cursor directly, which will contain the "_id" column.
(2) And here I am confused. Why getString method? As I understood to get some value from a column. But inside, the index of the DISPLAY_NAME_PRIMARY column is requested from the Cursor. But how? If the cursor only rows from the column "_id".
Please correct me where I am wrong.