简体   繁体   中英

How to view the checked checkbox in listview?

I am facing the issue in viewing the checked checkboxes in android

This is my code

  contactNumberList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {

            CheckBox checkBox = (CheckBox)view.findViewById(R.id.checkbox_number);

            AllContact contactNumber = contactsList.get(position);
            String num = contactNumber.getNumber();
            SelectedSpecificContact specificContact = new SelectedSpecificContact();
            specificContact.setNumber(num);

            if(!checkBox.isChecked())  {
                checkBox.setChecked(true);

                realmDbHelper.addSpecificSelectedNumber(specificContact);
                Log.e("number", num);
            }else{
                checkBox.setChecked(false);
                specificContact.setNumber(num);
                realmDbHelper.deleteAllSelectedData(specificContact);
            }
        }
    });

The list items checked are saved in a database.I want to know which items are checked in a listview. I have initialized the checkbox in listview on item click listener.It shows only the checked checkbox only when the current activity .If the activity is destroyed the checked check boxes are un checked.

How to view the checked check box when the activity appears.

Set the checkbox.setChecked(true); with condition where your database returns true condition. Suppose database returns true for the selection on day monday,

  if (monday == 1) {
       chkMonday.setChecked(true);
       Object.setMonday(1);
  } 

you can do this inside onCreate() or onResume()

Make a boolean or integer field in your bean class is_active like in SelectedSpecificContact.class . and set its value to 1 or true while checked and store in database.

Next time when you load them from database check if it is true or not.. if true set the drawable state as 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