ric
ric

Reputation: 13

How to save the state of the radio button android

First time asking question..How do i solve the problem of radio button unchecked when i scrolling in listview?(I am doing a simple survey app with 10questions)

MainActivity.java

public class MainActivity extends AppCompatActivity {

ListView listView;
String[] questions;
Button submit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    questions = getResources().getStringArray(R.array.questions);
    listView =(ListView) findViewById(R.id.listView);

    CustomAdapter customAdapter = new CustomAdapter(getApplicationContext(), questions);
    listView.setAdapter(customAdapter);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String message = "";
            // get the value of selected answers from custom adapter
            for (int i = 0; i < CustomAdapter.selectedAnswers.size(); i++) {
                message = message + "\n" + (i + 1) + " " + CustomAdapter.selectedAnswers.get(i);
            }
            // display the message on screen with the help of Toast.
            Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show();
        }
    });
}

CustomAdapter.java

public class CustomAdapter extends BaseAdapter {
Context context;
String[] questionsList;
LayoutInflater inflter;
public static ArrayList<String> selectedAnswers;




public CustomAdapter(Context applicationContext, String[] questionsList) {
    this.context = context;
    this.questionsList = questionsList;
    // initialize arraylist and add static string for all the questions
    selectedAnswers = new ArrayList<>();
    for (int i = 0; i < questionsList.length; i++) {
        selectedAnswers.add("Not Attempted");
    }
    inflter = (LayoutInflater.from(applicationContext));
}

@Override
public int getCount() {
    return questionsList.length;
}

@Override
public Object getItem(int i) {
    return null;
}

@Override
public long getItemId(int i) {
    return 0;
}

@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
    view = inflter.inflate(R.layout.questions, null);
    // get the reference of TextView and Button's
    TextView question = (TextView) view.findViewById(R.id.question);
    RadioButton radioButton = (RadioButton) view.findViewById(R.id.radioButton);
    RadioButton radioButton2 = (RadioButton) view.findViewById(R.id.radioButton2);
    RadioButton radioButton3 = (RadioButton) view.findViewById(R.id.radioButton3);
    RadioButton radioButton4 = (RadioButton) view.findViewById(R.id.radioButton4);
    RadioButton radioButton5 = (RadioButton) view.findViewById(R.id.radioButton5);

    // perform setOnCheckedChangeListener event on yes button
    radioButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set Yes values in ArrayList if RadioButton is checked
            if (isChecked)

                selectedAnswers.set(i, "");
        }
    });
    // perform setOnCheckedChangeListener event on no button
    radioButton2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set No values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(i, "");

        }
    });

    radioButton3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set No values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(i, "");

        }
    });

    radioButton4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set No values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(i, "");

        }
    });

    radioButton5.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // set No values in ArrayList if RadioButton is checked
            if (isChecked)
                selectedAnswers.set(i, "");

        }
    });
    // set the value in TextView
    question.setText(questionsList[i]);
    return view;
    }
}

Upvotes: 1

Views: 947

Answers (2)

Aman Srivastava
Aman Srivastava

Reputation: 973

I hope you have to use map instead of arraylist for storing selected answers because map will replace old value by new value on onCheckedStateChange.

Upvotes: 0

Magesh Pandian
Magesh Pandian

Reputation: 9369

Adapter getView called each time while scrolling so you need to main radio button isSeleted or not. See the link Listview radio button scroll issue.

Sample Code:

First create setter getter class for your questionList. (String[] questionsList);

 public class QuestionObject {

    private String question;
    private boolean isSelected;

    public QuestionObject(String question, boolean isSelected) {
        this.question = question;
        this.isSelected = isSelected;
    }

    public String getQuestion() {
        return question;
    }

    public boolean isSelected() {
        return isSelected;
    }

}

On Radiobutton onCheckedChanged

ArrayList<QuestionObject> mList = new ArrayList();
if(isChecked)
{
  mList.set(position, isChecked);
}

On getView

 QuestionObject obj = mList.get(i);
 mRadioBtn.setChecked(obj.isSelected());

Upvotes: 1

Related Questions