The fact is, when I check the CheckBox , the ID this item is saved in SQLite . There may be many. So when I launch the application back, then there is already only the last selected Checkbox selected, and the rest seems to be False;

In the adapter as follows:

 public class MainVacancyAdapter extends ArrayAdapter<VacancyModel> { private List<VacancyModel> vacancyModelList; private List<Integer> favVacanciesIDList; private int resource; private LayoutInflater inflater; SQLHelper sqlHelper; public MainVacancyAdapter(Context context, int resource, List<VacancyModel> objects) { super(context, resource, objects); vacancyModelList = objects; this.resource = resource; sqlHelper = new SQLHelper(getContext()); favVacanciesIDList = sqlHelper.getFavVacanciesID(); inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getView(final int position, View convertView, final ViewGroup parent) { final ViewHolder holder; if (convertView == null) { holder = new ViewHolder(); convertView = inflater.inflate(resource, null); holder.tvHeader = (TextView) convertView.findViewById(R.id.tvHeader); holder.tvDate = (TextView) convertView.findViewById(R.id.tvPostCr); holder.cbxFav = (CheckBox) convertView.findViewById(R.id.cbxFav); convertView.setTag(holder); } VacancyModel model = vacancyModelList.get(position); holder.tvHeader.setText(model.getHeader()); holder.tvDate.setText(model.getDate()); if (favVacanciesIDList != null){ for (int i = 0; i < favVacanciesIDList.size(); i++){ if (favVacanciesIDList.get(i) == vacancyModelList.get(position).getId()){ holder.cbxFav.setChecked(true); } else { holder.cbxFav.setChecked(false); } } } holder.cbxFav.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { int rowID = vacancyModelList.get(position).getId(); if (isChecked) { //inserting the data into Favourite Table // isSelected.set(position, true); vacancyModelList.get(position).setChecked(true); sqlHelper.createFavouriteTable(vacancyModelList.get(position)); } else { vacancyModelList.get(position).setChecked(false); sqlHelper.deleteFromFavouriteDatabase(rowID); notifyDataSetChanged(); } } }); return convertView; } private static class ViewHolder { private TextView tvHeader; private TextView tvDate; private CheckBox cbxFav; } } 

XML:

 <CheckBox android:adjustViewBounds="true" android:id="@+id/cbxFav" android:layout_height="40dp" android:layout_width="40dp" android:scaleType="fitCenter" android:layout_alignParentBottom="false" android:layout_alignParentRight="true" android:layout_alignParentEnd="true" android:layout_centerVertical="true" android:background="@drawable/custom_checkbox" android:button="@drawable/custom_checkbox_transparent" android:focusable="false"/> 

Here

 if (favVacanciesIDList != null){ for (int i = 0; i < favVacanciesIDList.size(); i++){ if (favVacanciesIDList.get(i) == vacancyModelList.get(position).getId()){ holder.cbxFav.setChecked(true); } else { holder.cbxFav.setChecked(false); } } } 

Everything is correct and compared if equal then сет . I did debugging.

Question: What could be the problem?

    1 answer 1

    You do not have the correct logic in determining the checkbox of the checkbox, I will try to explain. Suppose in favVacanciesIDList.get(3) you have a number that matches vacancyModelList.get(position).getId() , but only favVacanciesIDList 10 elements.
    Your cycle:

     for (int i = 0; i < favVacanciesIDList.size(); i++){ if (favVacanciesIDList.get(i) == vacancyModelList.get(position).getId()){ holder.cbxFav.setChecked(true); } else { holder.cbxFav.setChecked(false); } } 

    on the fourth iteration, your if condition will become true , and holder.cbxFav.setChecked(true); will execute holder.cbxFav.setChecked(true); . Then the fifth iteration will go, and holder.cbxFav.setChecked(false); will be holder.cbxFav.setChecked(false); That will make the checkbox unchecked.

    How to:

     holder.cbxFav.setChecked(false); //поумолчанию снимаем галочку for (int i = 0; i < favVacanciesIDList.size(); i++){ if (favVacanciesIDList.get(i) == vacancyModelList.get(position).getId()){ holder.cbxFav.setChecked(true); // если условие выполняется, то ставим галочку break; // и заканчиваем цикл. } } 

    UPD


    You can do more correctly - to store the CHECK elements in the Set , for example HashSet<Integer> . And the whole cycle is replaced by one line:

     holder.cbxFav.setChecked(favVacanciesIDSet.contains(vacancyModelList.get(position).getId())); 
    • Thanks a lot!!! I understood everything ) - DevOma
    • @TITAN, updated the answer. You can do even better - Vladyslav Matviienko
    • You know, I was busy with this for 3 weeks, in short, I am going to thump)) Thanks again !!! - DevOma
    • @TITAN, not at all, have a good rest! - Vladyslav Matviienko September