开发者

How do I set the default state of a checkbox in ListView [Android]

开发者 https://www.devze.com 2023-03-15 11:03 出处:网络
I am creating a list view as shown below. I want to set their states from a config file. How do I access the checkbox enabled property? I don\'t know how to get the checkbox object knowing o开发者_如何

I am creating a list view as shown below. I want to set their states from a config file. How do I access the checkbox enabled property? I don't know how to get the checkbox object knowing o开发者_如何学Gonly the text associated with it. I would like to loop through my configuration parameters and compare the config key to the items in the listview to obtain the index.

    ArrayList<String> chansModem0 = getChannelList();
    lv_arr = (String[]) chansModem0.toArray(new String[0]);

    lvModem0 = (ListView) findViewById(R.id.listViewModem0);
    lvModem0.setAdapter(new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_multiple_choice, lv_arr));

    lvModem0.setItemsCanFocus(false);
    lvModem0.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);


You have to override getView (int position, View convertView, ViewGroup parent) method in your adapter.

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = super.getView(position,convertView,parent);
    CheckBox cb = view.findViewById(R.id.check_box_id);
    boolean is_checked;
    /* check if the check box must be checked */
    cb.setChecked(is_checked);
}
0

精彩评论

暂无评论...
验证码 换一张
取 消