Android Radio buttons in a custom listview changes its state when we scroll the list

房东的猫 提交于 2019-11-30 21:27:27

You need to save the state of RadioButton for each item, so while scrolling in getView() method first it will check the state of each RadioButton.

Step1:- You need to create a Pojo class(Setter & Getter) for saving the state of RadioButton.

For more details go through the below link

http://amitandroid.blogspot.in/2013/03/android-listview-with-checkbox-and.html

This example is a ListView with CheckBox you need to change it to RadioButton according to your requirement.

You adapter is always recreating the item for the list view. When he does that, he takes a "fresh" view and you have to set the data to this view using your array.

In your case, if you want to "save" the state of the radio button, you have to save this data as part of the adapter data, meaning in the array. I suggest you add a field to your Option object and use it save the last value of the radio button. afterwards, when they line Option option = data[position]; is being called, you can then reclaim the last value and display it on screen.

Add to your adapter 2 abstract function below;

@Override
public int getCount() {
    return data.size(); // size, lenght, count ...?
}

@Override
public Object getItem(int position) {
    return position;
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!