Android listview multiple choice

后端 未结 3 1365
长发绾君心
长发绾君心 2020-12-18 11:56

How to get selected multichoice list view data. i have a listview with multiple choice. and i want to store selected item of listview in string array. can anyone guide me ho

3条回答
  •  囚心锁ツ
    2020-12-18 12:11

    Hey I have used String for saving all checked items from the list..See the code below:

    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.util.SparseBooleanArray;
    import android.view.View;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    
    
    public class ViewsActivity extends Activity 
    {
    
        private ListView lView;
        private String lv_items[] = { "Android", "iPhone", "BlackBerry",
                "AndroidPeople", "J2ME", "Listview", "ArrayAdapter", "ListItem",
                "Us", "UK", "India" };
        private String my_sel_items;
    
        @Override
        public void onCreate(Bundle icicle) {
            super.onCreate(icicle);
            setContentView(R.layout.main);
            my_sel_items=new String();
    
            lView = (ListView) findViewById(R.id.ListView01);
    
            lView.setAdapter(new ArrayAdapter(this,
                    android.R.layout.simple_list_item_multiple_choice, lv_items));
            lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    
            lView.setOnItemClickListener(new OnItemClickListener() {
    
                public void onItemClick(AdapterView arg0, View arg1, int arg2,long arg3)
                {
                    //List list = new ArrayList();
                    my_sel_items=new String("Selected Items");
                    SparseBooleanArray a = lView.getCheckedItemPositions();
    
                    for(int i = 0; i < lv_items.length ; i++)
                    {
                        if (a.valueAt(i))
                        {
                         /*
                            Long val = lView.getAdapter().getItemId(a.keyAt(i));
                            Log.v("MyData", "index=" + val.toString()
                                 + "item value="+lView.getAdapter().getItem(i));
                            list.add(lView.getAdapter().getItemId((a.keyAt(i))));
                         */
    
                            my_sel_items = my_sel_items + "," 
                                + (String) lView.getAdapter().getItem(i);
                        }
                    }
                    Log.v("values",my_sel_items);
                }
            });
        }
    }
    

提交回复
热议问题