gpt4 book ai didi

android - 每行有多个 radio 的 ListView

转载 作者:行者123 更新时间:2023-11-30 02:00:45 30 4
gpt4 key购买 nike

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

我现在发布我的代码

自定义测试适配器

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


}

最佳答案

问题出在adapter settag,你可以尝试下面的项目,因为它只是一个demo, https://drive.google.com/file/d/0B8QvcdrY5jP0c2p4UmlnNGc2dkU/view?usp=sharing

关于android - 每行有多个 radio 的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31528222/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com