- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.AdapterView.OnItemSelectedListener;
- import android.widget.ArrayAdapter;
- import android.widget.ListView;
- public class ListCheckbox extends Activity implements OnItemClickListener,OnItemSelectedListener{
- private String contentString[];
- ArrayAdapter arrayAdapter;
- ListView mylist;
- protected void onCreate(Bundle savedInstanceState) {
- // TODO Auto-generated method stub
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- contentString = new String[] {
- "示例", "透明动画",
- "伸缩动画", "移动动画",
- "旋转动画", "透明_伸缩",
- "透明_移动", "透明_旋转"
- };
- arrayAdapter = new ArrayAdapter<String>(this,
- android.R.layout.simple_list_item_multiple_choice,//.simple_list_item_single_choice,
- contentString);
- mylist = (ListView) findViewById(R.id.ListView01);
- mylist.setAdapter(arrayAdapter);
- mylist.setOnItemClickListener(this);
- mylist.setOnItemSelectedListener(this);
- mylist.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);//.CHOICE_MODE_SINGLE);
- mylist.setItemChecked(0, true);
- }
- public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
- mylist.setItemChecked(arg2, true);
- }
- public void onNothingSelected(AdapterView<?> arg0) {
- // TODO Auto-generated method stub
- }
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
- // TODO Auto-generated method stub
- }
- }
How to Get Checked Item list ?
SparseBooleanArray checked = list.getCheckedItemPositions(); for (int i = 0; i < list.getAdapter().getCount(); i++) { if (checked.get(i)) { // Do something } }
评论
发表评论