如何在ListView Android中检查RadioGroup中的单选按钮

时间:2017-04-02 13:42:23

标签: java android

我正在为测验开发应用程序。有十个问题,每个问题有四个答案A,B,C,D和代表RadioGroup,结束是提交按钮。问题列表由一个ListView表示。用户选择答案,然后单击“提交”按钮。我有一个问题:如何在ListView中依次检查答案?请帮我。非常感谢!

这是自定义问题的代码:

public class mAdapter extends ArrayAdapter<Question> {
    Activity context;
    int resource;
    List<Question> objects;

    public mAdapter(Activity context, int resource, List<Question> objects) {
        super(context, resource, objects);
        this.context = context;
        this.objects = objects;
        this.resource = resource;
    }

    @NonNull
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = this.context.getLayoutInflater();
        View row = inflater.inflate(this.resource, null);

        final Question question = this.objects.get(position);

        RadioGroup rg = (RadioGroup) row.findViewById(R.id.btnGroup);
        TextView txtID = (TextView) row.findViewById(R.id.txtID);
        TextView txtContent = (TextView) row.findViewById(R.id.txtContent);
        RadioButton btnA = (RadioButton) row.findViewById(R.id.btnA);
        RadioButton btnB = (RadioButton) row.findViewById(R.id.btnB);
        RadioButton btnC = (RadioButton) row.findViewById(R.id.btnC);
        RadioButton btnD = (RadioButton) row.findViewById(R.id.btnD);

        txtID.setText((position + 1) + "");
        txtContent.setText(question.getContent());
        btnA.setText(question.getOptionA());
        btnB.setText(question.getOptionB());
        btnC.setText(question.getOptionC());
        btnD.setText(question.getOptionD());

        return row;
    }

}

这是将问题推送到listView

的代码
lvQuestion= (ListView) findViewById(R.id.lvQuestion);
testAdapter = new mAdapter(this, R.layout.custom_lisview, listQuestion);
lvQuestion.setAdapter(testAdapter);

btnSubmit = (Button) findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
       @Override
       public void onClick(View v) {

       }
   });

1 个答案:

答案 0 :(得分:0)

radioButton

中处理listView
btnA.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked)
      btnA.setText(question.getOptionA());
     }
 });

btnB.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

if (isChecked)
     btnB.setText(question.getOptionB());
       }
});

// others code

通过OnClick

获得答案
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
     final int child=listView.getChildCount();
      for(int i=0;i<child;i++) {
        if(listView.childAt(i) instanceof RadioGroup){
         RadioGroup rg = (RadioGroup) row.findViewById(R.id.btnGroup);
         rg=(RadioGroup)listView.childAt(i);
         int a = rg.getChildCount();
         for(int j=0;j<a;++j) {
            if((RadioButton)rg.getChildAt(j).isChecked()==true) {
           System.out.println("RadioButton At "+j+" Is Selected");}
          }
       }
  }
}
});