Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The answer provided by Heejin is excellent, but it's not important to implement a custom ArrayAdaptor. What only I needed to do is to write a custom layout and send it to the <code>SimpleCursorAdaptor</code> constructor. </p> <p>The custom layout represent the layout of each item in the list view. I need each row to contain a <code>CheckedTextView</code> and another small <code>TextView</code> as a subitem. So I've made a layout called row_view.xml with the following content</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" &gt; &lt;CheckedTextView android:id="@+id/checkedTextView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:checkMark="?android:attr/listChoiceIndicatorMultiple" android:text="CheckedTextView" /&gt; &lt;TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Small Text" android:textAppearance="?android:attr/textAppearanceSmall" /&gt; &lt;/LinearLayout&gt; </code></pre> <p>Then I've just used it in the constructor</p> <pre><code>SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.row_view, // Use a template // that displays a // text view c, // Give the cursor to the list adapter new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, new int[] { R.id.checkedTextView, R.id.textView},0); setListAdapter(adapter); </code></pre> <p>This is the full code</p> <pre><code>public class MultipleContacts extends ListActivity implements OnItemClickListener { private static final String[] PROJECTION = new String[] { ContactsContract.CommonDataKinds.Phone._ID ,ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME ,ContactsContract.CommonDataKinds.Phone.NUMBER }; SimpleCursorAdapter adapter; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.multiple_contacts); getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); getListView().setOnItemClickListener(this); // Get a cursor with all people CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC"); Cursor c = cl.loadInBackground(); adapter = new SimpleCursorAdapter(this, R.layout.row_view, // Use a template // that displays a // text view c, // Give the cursor to the list adapter new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME, ContactsContract.CommonDataKinds.Phone.NUMBER}, new int[] { R.id.checkedTextView, R.id.textView},0); setListAdapter(adapter); } @Override public void onItemClick(AdapterView&lt;?&gt; parent, View v, int position, long id) { //what to do when an item is clicked CheckedTextView checkedTextView = (CheckedTextView) v.findViewById(R.id.checkedTextView); Toast.makeText(this, checkedTextView.getText(), Toast.LENGTH_SHORT).show(); } } </code></pre> <p>Please note that I've two layouts, one for the list view itself(called <code>multiple_contacts</code>) and the provided layout here (called <code>row_view</code>) is the layout for each item in the list view. All what I need from <code>multiple_contacts</code> is to write <code>setContentView(R.layout.multiple_contacts);</code></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload