listview,每行有多个无线电

时间:2015-07-20 23:56:41

标签: android listview

我的服务器提出了150个问题。我怎么似乎无法从每个位置选择的无线电组中的单选按钮获得结果。此外,当在数字1上选择1个按钮时,我们可以说数字5也具有相同的结果。

现在发布我的代码

customtestadapter

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;

public class customtestadapter extends BaseAdapter{
private ArrayList<testquestionlist> data;

String questionrewrite;





public customtestadapter(final ArrayList<testquestionlist> data)
    {
        this.data=data;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design

    ViewHolder viewHolder;
    if(convertView==null)
    {
        convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.test_row,parent, false);
        viewHolder=new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else
        viewHolder = (ViewHolder) convertView.getTag();

    viewHolder.questionnumber.setText("Question number : " + data.get(position).getQuestionid());
    questionrewrite = data.get(position).getQuestion();
    questionrewrite = questionrewrite.replaceAll("1:", System.getProperty("line.separator") + "1:");
    questionrewrite = questionrewrite.replaceAll("2:", System.getProperty("line.separator") + "2:");
    questionrewrite = questionrewrite.replaceAll("3:", System.getProperty("line.separator") + "3:");
    questionrewrite = questionrewrite.replaceAll("4:", System.getProperty("line.separator") + "4:");
    questionrewrite = questionrewrite.replaceAll("5:", System.getProperty("line.separator") + "5:");
    viewHolder.question.setText(questionrewrite);
    viewHolder.answeraread.setText(data.get(position).getAnswera());
    viewHolder.answerbread.setText(data.get(position).getAnswerb());
    viewHolder.answercread.setText(data.get(position).getAnswerc());
    viewHolder.answerdread.setText(data.get(position).getAnswerd());
    viewHolder.answereread.setText(data.get(position).getAnswere());

    viewHolder.answeraread.setChecked(data.get(position).getQuestionanswer() == "A");
    viewHolder.answerbread.setChecked(data.get(position).getQuestionanswer() == "B");
    viewHolder.answercread.setChecked(data.get(position).getQuestionanswer() == "C");
    viewHolder.answerdread.setChecked(data.get(position).getQuestionanswer() == "D");
    viewHolder.answereread.setChecked(data.get(position).getQuestionanswer() == "E");

    viewHolder.answeraread.setTag(position);
    viewHolder.answerbread.setTag(position);
    viewHolder.answercread.setTag(position);
    viewHolder.answerdread.setTag(position);
    viewHolder.answereread.setTag(position);

    convertView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ViewHolder vh = (ViewHolder) view.getTag();
            if (vh.answeraread.isChecked()){
                data.get(position).setQuestionanswer("A");
            }else if (vh.answerbread.isChecked()){
                data.get(position).setQuestionanswer("B");
            }else if (vh.answercread.isChecked()){
                data.get(position).setQuestionanswer("C");
            }else if (vh.answerdread.isChecked()){
                data.get(position).setQuestionanswer("D");
            }else if (vh.answereread.isChecked()){
                data.get(position).setQuestionanswer("E");
            }



            notifyDataSetInvalidated();
            notifyDataSetChanged();
        }
    });


    return convertView;
}



static class ViewHolder {
    TextView questionnumber;
    TextView question;
    RadioButton answeraread;
    RadioButton answerbread;
    RadioButton answercread;
    RadioButton answerdread;
    RadioButton answereread;
    RadioGroup questionanswers;
    public ViewHolder(final View view){
        questionnumber = (TextView) view.findViewById(R.id.questionnumber);
        question=(TextView) view.findViewById(R.id.question);
        answeraread=(RadioButton) view.findViewById(R.id.answeraread);
        answerbread=(RadioButton) view.findViewById(R.id.answerbread);
        answercread=(RadioButton) view.findViewById(R.id.answercread);
        answerdread=(RadioButton) view.findViewById(R.id.answerdread);
        answereread=(RadioButton) view.findViewById(R.id.answereread);
        questionanswers= (RadioGroup) view.findViewById(R.id.radioGroup1);
    }
}


}

2 个答案:

答案 0 :(得分:1)

问题在于适配器settag,您可以尝试以下项目,因为它只是一个演示, https://drive.google.com/file/d/0B8QvcdrY5jP0c2p4UmlnNGc2dkU/view?usp=sharing

答案 1 :(得分:0)

解决!

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;



public class customtestadapter extends BaseAdapter
{
private ArrayList<testquestionlist> data;

String questionrewrite;





public customtestadapter(final ArrayList<testquestionlist> data)
    {
        this.data=data;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return data.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design

    ViewHolder viewHolder;
    if(convertView==null)
    {
        convertView=LayoutInflater.from(parent.getContext()).inflate(R.layout.test_row,parent, false);
        viewHolder=new ViewHolder(convertView);
        convertView.setTag(viewHolder);
    } else
        viewHolder = (ViewHolder) convertView.getTag();

    viewHolder.questionnumber.setText("Question number : " + data.get(position).getQuestionid());
    questionrewrite = data.get(position).getQuestion();
    questionrewrite = questionrewrite.replaceAll("1:", System.getProperty("line.separator") + "1:");
    questionrewrite = questionrewrite.replaceAll("2:", System.getProperty("line.separator") + "2:");
    questionrewrite = questionrewrite.replaceAll("3:", System.getProperty("line.separator") + "3:");
    questionrewrite = questionrewrite.replaceAll("4:", System.getProperty("line.separator") + "4:");
    questionrewrite = questionrewrite.replaceAll("5:", System.getProperty("line.separator") + "5:");
    viewHolder.question.setText(questionrewrite);
    viewHolder.answeraread.setText(data.get(position).getAnswera());
    viewHolder.answerbread.setText(data.get(position).getAnswerb());
    viewHolder.answercread.setText(data.get(position).getAnswerc());
    viewHolder.answerdread.setText(data.get(position).getAnswerd());
    viewHolder.answereread.setText(data.get(position).getAnswere());
    if (data.get(position).getQuestionanswer() == "A") {
        viewHolder.answeraread.setChecked(true);
        viewHolder.answerbread.setChecked(false);
        viewHolder.answercread.setChecked(false);
        viewHolder.answerdread.setChecked(false);
        viewHolder.answereread.setChecked(false);
        viewHolder.answeraread.setTag(position);
        viewHolder.answerbread.setTag(position);
        viewHolder.answercread.setTag(position);
        viewHolder.answerdread.setTag(position);
        viewHolder.answereread.setTag(position);
    }
    if (data.get(position).getQuestionanswer() == "B") {
        viewHolder.answerbread.setChecked(true);
        viewHolder.answeraread.setChecked(false);
        viewHolder.answercread.setChecked(false);
        viewHolder.answerdread.setChecked(false);
        viewHolder.answereread.setChecked(false);
        viewHolder.answeraread.setTag(position);
        viewHolder.answerbread.setTag(position);
        viewHolder.answercread.setTag(position);
        viewHolder.answerdread.setTag(position);
        viewHolder.answereread.setTag(position);

    }
    if (data.get(position).getQuestionanswer() == "C") {
        viewHolder.answercread.setChecked(true);
        viewHolder.answerbread.setChecked(false);
        viewHolder.answeraread.setChecked(false);
        viewHolder.answerdread.setChecked(false);
        viewHolder.answereread.setChecked(false);
        viewHolder.answeraread.setTag(position);
        viewHolder.answerbread.setTag(position);
        viewHolder.answercread.setTag(position);
        viewHolder.answerdread.setTag(position);
        viewHolder.answereread.setTag(position);
    }
    if (data.get(position).getQuestionanswer() == "D") {
        viewHolder.answerdread.setChecked(true);
        viewHolder.answerbread.setChecked(false);
        viewHolder.answercread.setChecked(false);
        viewHolder.answeraread.setChecked(false);
        viewHolder.answereread.setChecked(false);
        viewHolder.answeraread.setTag(position);
        viewHolder.answerbread.setTag(position);
        viewHolder.answercread.setTag(position);
        viewHolder.answerdread.setTag(position);
        viewHolder.answereread.setTag(position);
    }
    if (data.get(position).getQuestionanswer() == "E") {
        viewHolder.answereread.setChecked(true);
        viewHolder.answerbread.setChecked(false);
        viewHolder.answercread.setChecked(false);
        viewHolder.answerdread.setChecked(false);
        viewHolder.answeraread.setChecked(false);
        viewHolder.answeraread.setTag(position);
        viewHolder.answerbread.setTag(position);
        viewHolder.answercread.setTag(position);
        viewHolder.answerdread.setTag(position);
        viewHolder.answereread.setTag(position);
    }
    if (data.get(position).getQuestionanswer() == null) {
        viewHolder.answeraread.setChecked(false);
        viewHolder.answerbread.setChecked(false);
        viewHolder.answercread.setChecked(false);
        viewHolder.answerdread.setChecked(false);
        viewHolder.answereread.setChecked(false);

        viewHolder.answeraread.setTag(position);
        viewHolder.answerbread.setTag(position);
        viewHolder.answercread.setTag(position);
        viewHolder.answerdread.setTag(position);
        viewHolder.answereread.setTag(position);

    }



    viewHolder.answeraread.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            data.get(position).setQuestionanswer("A");
            customtestadapter.this.notifyDataSetInvalidated();
            customtestadapter.this.notifyDataSetChanged();
        }
    });
    viewHolder.answerbread.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            data.get(position).setQuestionanswer("B");
            customtestadapter.this.notifyDataSetInvalidated();
            customtestadapter.this.notifyDataSetChanged();
        }
    });
    viewHolder.answercread.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            data.get(position).setQuestionanswer("C");
            customtestadapter.this.notifyDataSetInvalidated();
            customtestadapter.this.notifyDataSetChanged();
        }
    });
    viewHolder.answerdread.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            data.get(position).setQuestionanswer("D");
            customtestadapter.this.notifyDataSetInvalidated();
            customtestadapter.this.notifyDataSetChanged();
        }
    });
    viewHolder.answereread.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            data.get(position).setQuestionanswer("E");
            customtestadapter.this.notifyDataSetInvalidated();
            customtestadapter.this.notifyDataSetChanged();
        }
    });




    return convertView;
}



static class ViewHolder {
    TextView questionnumber;
    TextView question;
    RadioButton answeraread;
    RadioButton answerbread;
    RadioButton answercread;
    RadioButton answerdread;
    RadioButton answereread;
    RadioGroup questionanswers;
    public ViewHolder(final View view){
        questionnumber = (TextView) view.findViewById(R.id.questionnumber);
        question=(TextView) view.findViewById(R.id.question);
        answeraread=(RadioButton) view.findViewById(R.id.answeraread);
        answerbread=(RadioButton) view.findViewById(R.id.answerbread);
        answercread=(RadioButton) view.findViewById(R.id.answercread);
        answerdread=(RadioButton) view.findViewById(R.id.answerdread);
        answereread=(RadioButton) view.findViewById(R.id.answereread);
        questionanswers= (RadioGroup) view.findViewById(R.id.radioGroup1);
    }
}


}