Flags in the list are randomly checked / not checked when scrolling. Android 2.3

I am using my custom adapter. Each line has a check box and a text view. But I have a problem. The list contains more items than the number that fits the screen.

Therefore, when I check any flag on the screen and scroll down. Automatically uncontrolled.

When I scroll again, some random flags are checked.

I know that in the list getView () is updated again and again, and the problem is in the position. But I do not know the solution.

I tried using a boolean array to save the track. but dnt knows how to do it right.

cb.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { itemChecked[index]=isChecked; } }); cb.setChecked(itemChecked[index]); 

Can you post a snippet of code and explain it in detail. I am tired of looking for this since the last 2 weeks ... thanks in advance, ..

+1
source share
2 answers
  public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { final ViewHolder holder; //if (convertView == null) { holder = new ViewHolder(); convertView = mInflater.inflate( R.layout.expandableinner_custom_row, null); holder.textView = (TextView) convertView .findViewById(R.id.textView); holder.checkBox = (CheckBox) convertView .findViewById(R.id.checkBox); String str = getChild(groupPosition, childPosition).toString(); holder.textView.setText(str); Boolean[] boolArr = SMSActivity.maintaingState.get(String .valueOf(groupPosition)); Log.i("BOOLARRAYSIZE ", String.valueOf(boolArr.length)); Log.i("Boolean", String.valueOf(boolArr[childPosition] + "GrpPosition: " + groupPosition + "ChildPosition: " + childPosition)); if (boolArr[childPosition]) { holder.checkBox.setChecked(true); } else { holder.checkBox.setChecked(false); } holder.checkBox .setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged( CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub Boolean[] boolArr = SMSActivity.maintaingState .get(String.valueOf(groupPosition)); boolArr[childPosition] = isChecked;// (object) Log.i("grpPosition", String.valueOf(childPosition)); Log.i("childPosition", String.valueOf(groupPosition)); SMSActivity.maintaingState.remove(String .valueOf(groupPosition)); SMSActivity.maintaingState.put( String.valueOf(groupPosition), boolArr); Log.i("Boolean", String .valueOf(boolArr[childPosition] + " GrpPosition: " + groupPosition + "ChildPosition: " + childPosition)); } }); convertView.setTag(holder); return convertView; } 
+6
source

set each checkbox position using the setup method (position) in the getview () method, and by clicking each checkbox, set the position (Integer) buttonView.getTag (). I think the code below will solve your problem.

  public View getView(int position, View convertView, ViewGroup parent) { holder=new ViewHolder(); convertView=lInflater.inflate(R.layout.extraevents, null); holder.txtTitle=(TextView)convertView.findViewById(R.id.eventtitle); holder.txtLocation=(TextView)convertView.findViewById(R.id.eventLoc); holder.ChkEachBox=(CheckBox)convertView.findViewById(R.id.CheckBoxSelected); holder.ChkEachBox.setClickable(true); holder.ChkEachBox.setFocusable(false); holder.ChkEachBox.setTag(position); holder.ChkEachBox.setOnCheckedChangeListener(this); EventItems cart=eventList.get(position); holder.txtTitle.setText(cart.title); holder.txtLocation.setText(cart.location); if(cart.selected==true) { holder.ChkEachBox.setChecked(true); } else { holder.ChkEachBox.setChecked(false); } return convertView; } public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { EventItems cart=eventList.get((Integer)buttonView.getTag()); if(buttonView.isChecked()) { cart.selected = true; } else { cart.selected = false; } } 
+4
source

Source: https://habr.com/ru/post/892363/


All Articles