ListView.CHOICE_MODE_MULTIPLE : ListView « UI « Android






ListView.CHOICE_MODE_MULTIPLE

   

package app.test;

import android.app.ListActivity;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Test extends ListActivity
{
    private static final String TAG = "ListViewActivity4";
    private static final Uri CONTACTS_URI = ContactsContract.Contacts.CONTENT_URI;
  private SimpleCursorAdapter adapter = null;
    private ListView lv = null;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        lv = getListView();

    String[] projection = new String[]{ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME};
        Cursor c = managedQuery(CONTACTS_URI,
                        projection, null, null, ContactsContract.Contacts.DISPLAY_NAME);

        String[] cols = new String[]{ContactsContract.Contacts.DISPLAY_NAME};
        int[]   views = new int[]   {android.R.id.text1};

        adapter = new SimpleCursorAdapter(this,
            android.R.layout.simple_list_item_multiple_choice,
            c, cols, views);

        this.setListAdapter(adapter);

        lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
    
    public void doClick(View view) {
      if(!adapter.hasStableIds()) {
        Log.v(TAG, "Data is not stable");
        return;
      }
        long[] viewItems = lv.getCheckedItemIds();
        for(int i=0; i<viewItems.length; i++) {
        Uri selectedPerson = ContentUris.withAppendedId(
            CONTACTS_URI, viewItems[i]);

        Log.v(TAG, selectedPerson.toString() + " is checked.");
        }
    }
}



//main.xml

<?xml version="1.0" encoding="utf-8"?>
<!-- This file is at /res/layout/list.xml -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"  android:layout_height="fill_parent">

    <ListView android:id="@android:id/list"
        android:layout_width="fill_parent"  android:layout_height="0dip"
        android:layout_weight="1" />

    <Button android:id="@+id/btn" android:onClick="doClick"
     android:layout_width="wrap_content"  android:layout_height="wrap_content"
     android:text="Submit Selection" />

</LinearLayout>

   
    
    
  








Related examples in the same category

1.Using ExpandableListView
2.Using ListView
3.Using SimpleAdapter to fill data to ListView
4.Custom cell Renderer for ListView
5.Provide xml layout for ListView Item
6.Fill contact information to ListView
7.Use AbsListView OnScrollListener(AbsListView.OnScrollListener), AbsListView#setOnItemScrollListener(AbsListView.OnItemScrollListener)} to display the first letter of the visible range of cheeses.
8.This demo illustrates the use of CHOICE_MODE_MULTIPLE_MODAL, a.k.a. selection mode on ListView.
9.FileList extends ListView
10.set ListView Height Based On Children
11.Adding a List
12.Lunch List
13.Get Item index in item click event
14.On nothing selected event
15.Create List