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

前端 未结 3 528
刺人心
刺人心 2021-01-05 22:01

I am a beginner and facing a problem where I am creating a ListView with two RadioButtons and a TextView. Everything goes fine, but wh

相关标签:
3条回答
  • 2021-01-05 22:23

    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.

    0 讨论(0)
  • 2021-01-05 22:31

    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;
    }
    
    0 讨论(0)
  • 2021-01-05 22:36

    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.

    0 讨论(0)
提交回复
热议问题