如何保存单选按钮android的状态

时间:2016-11-02 07:34:46

标签: android android-radiobutton

第一次询问问题。当我在listview中滚动时,如何解决单选按钮未选中的问题?(我正在做一个带10个问题的简单调查应用程序)

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;
    }
}

2 个答案:

答案 0 :(得分:1)

每次滚动时调用适配器getView,因此您需要主单选按钮是否已选中。请参阅链接Listview radio button scroll issue

示例代码:

首先为questionList创建setter getter类。 (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;
    }

}

关于Radiobutton onCheckedChanged

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

在getView上

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

答案 1 :(得分:0)

我希望你必须使用map而不是arraylist来存储选定的答案,因为map会用onCheckedStateChange上的新值替换旧值。