Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One quick way would be to use <code>android.R.layout.simple_list_item_multiple_choice</code>. This code example does what you want (just note that I used Contacts for my Cursor):</p> <pre><code>public class ListCheck extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.list_check); this.getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE); Cursor cursor = managedQuery(ContactsContract.Contacts.CONTENT_URI, null, null, null, null); startManagingCursor(cursor); String[] columns = new String[] { ContactsContract.Contacts.DISPLAY_NAME }; int[] to = new int[] { android.R.id.text1 }; SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, cursor, columns, to); this.setListAdapter(adapter); Button finishButton = (Button) this.findViewById(R.id.finishButton); finishButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { SimpleCursorAdapter adapter = (SimpleCursorAdapter) ListCheck.this.getListAdapter(); Cursor cursor = adapter.getCursor(); ListView lv = ListCheck.this.getListView(); SparseBooleanArray selectedItems = lv.getCheckedItemPositions(); for (int i = 0; i &lt; selectedItems.size(); i++) { int selectedPosition = selectedItems.keyAt(i); cursor.moveToPosition(selectedPosition); Log.d("", cursor.getString(cursor.getColumnIndex( ContactsContract.Contacts.DISPLAY_NAME))+" is checked"); Log.d("", "row id: "+adapter.getItemId(selectedPosition)); } } }); } } </code></pre> <p>Activity layout:</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="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;Button android:id="@+id/finishButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Button" /&gt; &lt;ListView android:id="@android:id/list" android:layout_width="fill_parent" android:layout_height="wrap_content" android:choiceMode="multipleChoice" /&gt; &lt;/LinearLayout&gt; </code></pre> <hr> <p>However, if you want to add other custom data inside each row (image, subtitle..), then you have to create a custom layout for your rows using <a href="http://developer.android.com/reference/android/widget/CheckedTextView.html" rel="nofollow"><code>CheckedTextView</code></a>. <code>CheckedTextView</code> is really the key here, since it's used by the <a href="https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_multiple_choice.xml" rel="nofollow"><code>android.R.layout.simple_list_item_multiple_choice</code></a>.</p> <p>Just follow this example:</p> <pre><code>&lt;com.mfp.tests.CheckableLinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" &gt; &lt;CheckedTextView android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_vertical" android:checkMark="?android:attr/listChoiceIndicatorMultiple" /&gt; &lt;TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="added a data: subtitle"/&gt; &lt;/com.mfp.tests.CheckableLinearLayout&gt; </code></pre> <p>One last thing: I used a custom layout instead of a <code>LinearLayout</code>. In order to make the checkBox of <code>CheckedTextView</code> responsive (automatically checked or unchecked when you click on a row), the layout must implement <code>Checkable</code> (and LinearLayout doesn't), so you'll have to copy the <code>CheckableLinearLayout</code> class from this <a href="http://tokudu.com/2010/android-checkable-linear-layout/" rel="nofollow">link</a>:</p> <p>PS: If you want to try the code above, don't forget to put <code>&lt;uses-permission android:name="android.permission.READ_CONTACTS" /&gt;</code> inside your Manifest.xml</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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