我有一个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) {
}
}