简体   繁体   中英

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

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 only 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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM