ListAdapter adapter new SimpleCursorAdapter this contacts

Listadapter adapter new simplecursoradapter this

This preview shows page 59 - 66 out of 78 pages.

ListAdapter adapter = new SimpleCursorAdapter( this, R.layout.scratch_layout, contacts, new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER }, new int[] { R.id.scratch_text1, R.id.scratch_text2 }, 0);
Image of page 59

Subscribe to view the full document.

Using CursorAdapter (Cont’d) This example above demonstrates creating a CursorAdapter by querying the Contacts content provider. The CursorAdapter requires the use of a Cursor . Couple of new concepts - First, you need to know that the Cursor must contain a field named _id . In this case, we know that the ContactsContract content provider does have this field. This field is used later when we handle the user selecting a particular item. We instantiate a new CursorLoader to get the Cursor . Then we instantiate a SimpleCursorAdapter as a ListAdapter . Our layout, R.layout.scratch_layout , has two TextView controls in it, which are used in the last parameter. SimpleCursorAdapter enables us to match up columns in the database with particular controls in our layout. For each row returned from the query, we get one instance of the layout within our AdapterView .
Image of page 60
Binding Data to the AdapterView Now that you have an Adapter object, you can apply this to one of the AdapterView controls. Either of them will work. Here is an example of this with a ListView , continuing on from the previous sample code: ((ListView)findViewById(R.id.scratch_adapter_view)) .setAdapter(adapter); The call to the setAdapter() method of the AdapterView , a ListView in this case, should come after your call to setContentView() . This is all that is required to bind data to your AdapterView .
Image of page 61

Subscribe to view the full document.

Binding Data to the AdapterView (Cont’d)
Image of page 62
Handling Selection Events You often use AdapterView controls to present data from which the user should select. Both of the discussed controls ListView and GridView enable your application to monitor for click events in the same way. You need to call setOnItemClickListener() on your AdapterView and pass in an implementation of the AdapterView.OnItemClickListener class.
Image of page 63

Subscribe to view the full document.

Handling Selection Events (Cont’d) av.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick( AdapterView<?> parent, View view, int position, long id) { Toast.makeText(Scratch.this, "Clicked _id="+id, Toast.LENGTH_SHORT).show(); } });
Image of page 64
Handling Selection Events (Cont’d) In the preceding example, av is our AdapterView . The implementation of the onItemClick( ) method is where all the interesting work happens. The parent parameter is the AdapterView where the item was clicked. This is useful if your screen has more than one AdapterView on it. The View parameter is the specific View within the item that was clicked. The position is the zero-based position within the list of items that the user selects. Finally, the id parameter is the value of the _id column for the item that the user selects. This is useful for querying for further information about the row of data that the item represents.
Image of page 65

Subscribe to view the full document.

Image of page 66
  • Summer '16