how to add a checkbox in a listview?

后端 未结 5 1144
长发绾君心
长发绾君心 2021-01-06 15:34

i have a question, been stuck for a while, i dont know how can i add a checkbox in the list, for example if I have a list of items i want to be able to check them. my xml c

相关标签:
5条回答
  • 2021-01-06 16:07

    If you use a ListAdapter (e.g. a customized BaseAdapter) on your listView with setAdapter() you can use LayoutInflater inside your adapter's getView() to supply any layout you like for your list entries. This can include a CheckBox.

    0 讨论(0)
  • 2021-01-06 16:10

    I recommend you watch this video from the android dev site on how to Make your Android UI Fast and Efficient. It will give you code to solve your problem and show you the proper way to implement your adapter to make sure it's as fast as can be.

    0 讨论(0)
  • 2021-01-06 16:18
    final ListView lView = (ListView) findViewById(R.id.ListView01);
    lView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, items));
    lView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    

    Here item is an array.

    0 讨论(0)
  • 2021-01-06 16:19

    Or you can extends any ListAdapter to a subclass and override bindView. Inside of if you would set .setText for the ChecBoxes!

    0 讨论(0)
  • 2021-01-06 16:23
    public class myAdapter extends SimpleCursorAdapter {
        public myAdapter(Context context, int layout, Cursor cursor, String[] from, int[] to) {
            super(context, layout, cursor, from, to);  
    }
    
    @Override   
    public void bindView(View view, Context context, Cursor cursor) {
        cb=(CheckBox)view.findViewById(R.id.cb);
        cb.setText(dbgetStringValue);
        cbText=(TextView)view.findViewById(R.id.cbText);
                cbText.setText(dbgetStringValue);
        cb.setChecked(false);
        cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton cb, boolean isChecked) {            
                if(cb.isChecked()) {                    
                    // action
                }
                else if(isChecked==false) {
                    // action
                }
            }           
        });
    }
    } 
    

    is that what you want?

    0 讨论(0)
提交回复
热议问题