Saturday, 13 December 2014

Stop Refreshing on List View With Check Box / Radio Button

Stop Refreshing on List View With Check Box / Radio Button

Hi!

                 In our Development, Mostly Developers facing one common problem while developing listview with CheckBox / Radio Buttons.

       After you load your data on Listview, when you scroll the listview the selected items will disappear and some items will selected automatically. this problem is because of we are try to figure out the item by position. But when the listview is scrolled up / down the position will be reset.

The solution is we have to take the components tag position by getTag(), and we can identify by this tag position. But should set the Tag before set the value on the Item.

On Business Class

boolean P_is_checked;

i have create a boolean value to carry the state.

On The Adapter Class
{
// radio button on checkedchangelistener
rb_p.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
//  Step - 1 get the tag position while it is checked
int getPosition = (Integer) buttonView.getTag(); 
                               // Step - 2 On The Business Class Object set the boolean value state
A_list.get(getPosition).setP_is_checked(buttonView.isChecked());
}
});
// Step - 3 set the Tag position
rb_p.setTag(position);
//Step - 4 Set the value of the item
rb_p.setChecked(A_list.get(position).P_is_checked);
}

Thats all, no your listview the CheckBox / RadioButton will never check or unchecked automatically while scrolling..,


Enjoy..,

Thank You!

Please Leave Your Comment..,


Have A Happy Day..,

6 comments:

  1. In listview srolling checkboxes disappear to all checked checkboexs.Here is my code to select all check boxes
    selectAll.setOnClickListener(new OnClickListener() {

    @SuppressLint("NewApi") public void onClick(View v) {
    try
    {
    int listCount =myValuesArr.size();
    if(listCount > 0)
    {

    for (int i = 0; i < listCount; i++)
    {
    View view = (View) listView.getChildAt(i);
    if(view !=null)
    check= (CheckBox)view.findViewById(R.id.selectRecord);
    if(selectAll.isChecked())
    {log.debug("checkd Item"+i);
    check.setChecked(true);
    }
    else
    {log.debug("uncheck Item"+i);
    check.setChecked(false);
    }

    }

    }
    else{
    Toast.makeText(getApplicationContext(), "No Records to Select", Toast.LENGTH_SHORT).show();
    }
    } catch (Exception e) {
    log.error( TFUtils.getErrorMessage(e));
    }
    }
    });

    ReplyDelete
    Replies
    1. Hi Brother!

      Sorry now only i saw your comment. Just add TAG thats enough..,

      Thank You..,

      Delete
  2. I have created a list view with spinner. I want to use spinner without refreshing...kindly suggest me any solution

    ReplyDelete
  3. sir could you check on this?? https://stackoverflow.com/questions/59812051/selecting-one-from-radio-group-value-and-scrolling-down-and-some-item-selected-a

    i am having a same problem.

    ReplyDelete