Thursday, September 22, 2011

Multi Choice ListView

Multi Choice ListView



package com.exercise.AndroidMultiChoiceList;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AndroidMultiChoiceListActivity extends Activity {
 
 ListView choiceList;
 String[] choice = { "Choice A", 
   "Choice B", "Choice C", "Choice D", "Choice E"};
 
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        choiceList = (ListView)findViewById(R.id.list);
        
        ArrayAdapter<String> adapter
        = new ArrayAdapter<String>(this, 
          android.R.layout.simple_list_item_multiple_choice, 
          choice);
        choiceList.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
        choiceList.setAdapter(adapter);
 
    }
}


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="@string/hello"
    />
<ListView 
    android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    />
</LinearLayout>


Related:
- Implement multi select ListView with custom layout, using custom ArrayAdapter.

Related ListFragment:
ListFragment with multiple choice

No comments: