Islam Hassan
Islam Hassan

Reputation: 1736

Android: ListView with multiple choice and subitems enabled

I'm trying to get my contacts on a list view. Now I know that using simple_list_item_multiple_choice enables me to select multiple contacts, but it views names only without numbers.

On the other side, simple_list_item_2 can be used to show both name and number, but supports selection of only one contact.

Is there any template that combine them both? If not, how could I build my customized list with both features?

EDIT: This is the code I'm using

CursorLoader cl = new CursorLoader(this,ContactsContract.CommonDataKinds.Phone.CONTENT_URI, PROJECTION, null, null, ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME+" ASC");
Cursor c = cl.loadInBackground();
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_multiple_choice, // Use a template
                                                         // that displays a
                                                         // text view
                    c, // Give the cursor to the list adapter
                    new String[] { ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME},
                    new int[] { android.R.id.text1},0); 

setListAdapter(adapter);

Here, the second parameter of SimpleCursorAdapter is simple_list_item_multiple_choice but it supports only dealing with android.R.id.text1. So I can use items only, not subitems.

But in the following code

SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_expandable_list_item_2, // 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[] { android.R.id.text1,android.R.id.text2},0); 

I can give it both ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME and NUMBER to be written in android.R.id.text1 and android.R.id.text2, but can't use multiple choice feature.

Upvotes: 1

Views: 3314

Answers (2)

Islam Hassan
Islam Hassan

Reputation: 1736

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 SimpleCursorAdaptor constructor.

The custom layout represent the layout of each item in the list view. I need each row to contain a CheckedTextView and another small TextView as a subitem. So I've made a layout called row_view.xml with the following content

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <CheckedTextView
        android:id="@+id/checkedTextView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:checkMark="?android:attr/listChoiceIndicatorMultiple"
        android:text="CheckedTextView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

Then I've just used it in the constructor

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);

This is the full 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<?> 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();
    }   
}

Please note that I've two layouts, one for the list view itself(called multiple_contacts) and the provided layout here (called row_view) is the layout for each item in the list view. All what I need from multiple_contacts is to write setContentView(R.layout.multiple_contacts);

Upvotes: 0

Heejin
Heejin

Reputation: 4571

As Dipu said, you should make your own customized layout. To show name and contact, you need two text views, and one check box for checking.

You can start coding from this tutorial:

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

Add one more text view to country_info.xml will solve your problem.

ADDED

To use a custom list view layout, you have to implement your own adapter.

This tutorial (2. Custom ArrayAdapter example) will help you figure out how to do that.

http://www.mkyong.com/android/android-listview-example/

Upvotes: 2

Related Questions