There is a ListView containing an array of names that is loaded from an XML file. The ListView unit is a CheckedTextView item.

When the Activity starts, it connects to the device, receiving an array of bytes equivalent to an array of names. The resulting array contains information about the inclusion of a particular name, which should be expressed in the presence or absence of a check box.

The question is how to initialize the entire list of values ​​on / off (checkbox set / unchecked)?

My attempts to initialize values:

for(int i = 0; i <= choiceModeList.getLastVisiblePosition() - choiceModeList.getFirstVisiblePosition(); i++) { CheckedTextView chkd = choiceModeList.getChildAt(i).findViewById(android.R.id.text1); chkd.setChecked(data[i]); } 

In this case, checkboxes are set, but only for the current view (in the case of scrolling, the values ​​disappear).

Then I tried to create my ArrayAdapter and override the getView method

  //data - массив содержащий актуальное состояние чекбоксов для каждого элемента списка public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { CheckedTextView checkbox; View tempView = convertView; if(tempView == null) { LayoutInflater inflater = context.getLayoutInflater(); tempView = inflater.inflate(android.R.layout.simple_list_item_multiple_choice, null, true); checkbox = tempView.findViewById(android.R.id.text1); CharSequence[] chars = names; checkbox.setText(chars[position]); tempView.setTag(checkbox); } else { checkbox = (CheckedTextView) tempView.getTag(); } if(data != null) { if (data[position] == 1) { checkbox.setChecked(true); } else { checkbox.setChecked(false); } } return tempView; } 

It seems to me that I am missing something obvious and important. Probably, I somehow not so google this question. If you have a tip, or a link to a material / article in Russian / English, then I will greatly appreciate your help! Thank you in advance!

UPD : I watched the recording of the Google I / O conference from 2009. There this moment was disassembled in sufficient detail. Perhaps someone will come in handy: https://youtu.be/N6YdwzAvwOA?t=119

  • I watched a lecture of 2009 at the Google conference. There it was just said at the beginning. Everything has become much clearer. - GuhaFun

0