我有custom listview
如图所示。以下是custom listview
public class CustomArrayAdapter extends BaseAdapter implements
OnCheckedChangeListener {
private Context context;
private ArrayList<HashMap<String, String>> data;
HashMap<String, String> hMap;
String question, opt1, opt2, opt3, opt4, value;
String[] answer;
QuestionAnswer qa = new QuestionAnswer();
public CustomArrayAdapter(Context context,
ArrayList<HashMap<String, String>> d) {
super();
this.context = context;
data = d;
}
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.custom_questions, parent,
false);
TextView tv = (TextView) rowView.findViewById(R.id.textView1);
RadioGroup rg = (RadioGroup) rowView.findViewById(R.id.radioGroup1);
final RadioButton rb1 = (RadioButton) rowView.findViewById(R.id.radio0);
final RadioButton rb2 = (RadioButton) rowView.findViewById(R.id.radio1);
final RadioButton rb3 = (RadioButton) rowView.findViewById(R.id.radio2);
final RadioButton rb4 = (RadioButton) rowView.findViewById(R.id.radio3);
hMap = new HashMap<String, String>();
hMap = data.get(position);
tv.setText(hMap.get("questions"));
question = hMap.get("questions");
/*Iterator myVeryOwnIterator = hMap.keySet().iterator();
while (myVeryOwnIterator.hasNext()) {
String key = (String) myVeryOwnIterator.next();
value = (String) hMap.get("questions");
System.out.println("mother fucker " + value);
qa.setQuestion(value);
questions(value);
}*/
rb1.setText(hMap.get("op1"));
opt1 = hMap.get("op1");
rb2.setText(hMap.get("op2"));
opt2 = hMap.get("op2");
rb3.setText(hMap.get("op3"));
opt3 = hMap.get("op3");
rb4.setText(hMap.get("op4"));
opt4 = hMap.get("op4");
rg.setOnCheckedChangeListener(this);
return rowView;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
int childCount = group.getChildCount();
for (int x = 0; x < childCount; x++) {
RadioButton btn = (RadioButton) group.getChildAt(x);
if (btn.getId() == checkedId) {
String ans = btn.getText().toString();
}
}
}
}
用户可以点击任何选项。但我需要获得final value
selected
。如果用户选择Anil然后选择John并移动到第二个问题,他选择Sick然后选择Play。我需要得到这些最终答案,即John和Play。我该怎么做呢 ?请帮忙。
这就是我填充custom listview
的方式。
public class Account extends ListFragment implements OnItemClickListener {
ArrayList<HashMap<String, String>> radioData = new ArrayList<HashMap<String, String>>();
ListView lv;
RadioGroup rg;
Data data;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.account, container, false);
final TextView tv = (TextView)rootView.findViewById(R.id.textView1);
final Button bStart = (Button)rootView.findViewById(R.id.button1);
final Button bSubmit = (Button)rootView.findViewById(R.id.button2);
lv = (ListView) rootView.findViewById(android.R.id.list);
rg = (RadioGroup)rootView.findViewById(R.id.radioGroup1);
bStart.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
});
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
String questions[] = { "what is your name", "why are you here" };
String op1[] = { "Anil", "Play" };
String op2[] = { "John", "Sick" };
String op3[] = { "David", "Write" };
String op4[] = { "Akshaya", "Act" };
for (int i = 0; i < questions.length; i++) {
HashMap<String, String> hMap1 = new HashMap<String, String>();
hMap1.put("questions", questions[i]);
hMap1.put("op1", op1[i]);
hMap1.put("op2", op2[i]);
hMap1.put("op3", op3[i]);
hMap1.put("op4", op4[i]);
radioData.add(hMap1);
}
/*ListAdapter adapter = new SimpleAdapter(getActivity(), radioData,
R.layout.custom_questions, new String[] { "questions", "op1",
"op2", "op3", "op4" }, new int[] { R.id.textView1,
R.id.radio0, R.id.radio1, R.id.radio2, R.id.radio3 });*/
CustomArrayAdapter adap = new CustomArrayAdapter(getActivity(), radioData);
lv.setAdapter(adap);
lv.setOnItemClickListener(this);
return rootView;
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
}
}
答案 0 :(得分:1)
您可以在提交按钮的onClick中执行此操作,如下所示:
bSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Id of checked radio button
int checkedId = rgroup.getCheckedRadioButtonId();
//Text of that id
if(checkedId > 0) { //condition to check that at radio button is selected.
RadioButton radioButton = (RadioButton) findViewById(checkedId);
String text = radioButton.getText().toString();
}
}
});
希望它有所帮助。
答案 1 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
选择一个 HashMap ,其中 Interger 为关键, String 为值,并尝试将所有四个 RadioButton ID作为键和 RadioButton 文本作为值并尝试设置将此 HashMap 标记为 RadioGroup 。当任何 RadioButton 检查更改时监听器从 RadioGroup 标签中调用 HashMap ,并使用选中的 RadioButton >从 HashMap 中选中 RadioButton 文本strong> id。
public class CustomArrayAdapter extends BaseAdapter{
private Context context;
private ArrayList<HashMap<String, String>> data;
public CustomArrayAdapter(Context context,ArrayList<HashMap<String, String>> data) {
super();
this.context = context;
this.data = data;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.custom_questions, parent,false);
holder.textView1 = (TextView) convertView.findViewById(R.id.textView1);
holder.radioGroup1 = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
holder.radio0 = (RadioButton) convertView.findViewById(R.id.radio0);
holder.radio1 = (RadioButton) convertView.findViewById(R.id.radio1);
holder.radio2 = (RadioButton) convertView.findViewById(R.id.radio2);
holder.radio3 = (RadioButton) convertView.findViewById(R.id.radio3);
convertView.setTag(holder);
}else{
holder =(ViewHolder) convertView.getTag();
}
holder.textView1.setText(data.get(position).get("questions"));
holder.radio0.setText(data.get(position).get("op1"));
holder.radio1.setText(data.get(position).get("op2"));
holder.radio2.setText(data.get(position).get("op3"));
holder.radio3.setText(data.get(position).get("op4"));
HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
radioMap.put(holder.radio0.getId(),holder.radio0.getText().toString());
radioMap.put(holder.radio1.getId(),holder.radio1.getText().toString());
radioMap.put(holder.radio2.getId(),holder.radio2.getText().toString());
radioMap.put(holder.radio3.getId(),holder.radio3.getText().toString());
holder.radioGroup1.setTag(radioMap);
holder.radioGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
HashMap<Integer,String> data = (HashMap<Integer,String>) group.getTag();
Toast.makeText(context,data.get(checkedId),Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return data.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
class ViewHolder{
TextView textView1;
RadioGroup radioGroup1;
RadioButton radio0;
RadioButton radio1;
RadioButton radio2;
RadioButton radio3;
}
}