这是通过扩展 BaseAdapter 的自定义列表视图。我需要获取所选单选按钮的值,但我不知道如何。所以请帮助我。下面是自定义适配器类。
public class CustomArrayAdapter extends BaseAdapter implements OnCheckedChangeListener {
private Context context;
private String[] q,o1,o2,o3,o4;
private int count=0;
private ArrayList<HashMap<String, String>> data ;
public CustomArrayAdapter(Context context, ArrayList<HashMap<String, String>> d) {
super();
this.context = context;
data = d;
count++;
System.out.println("adhe padhe nuna chate");
}
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);
HashMap<String, String> hMap = new HashMap<String,String>();
hMap = data.get(position);
tv.setText(hMap.get("questions"));
rb1.setText(hMap.get("op1"));
rb2.setText(hMap.get("op2"));
rb3.setText(hMap.get("op3"));
rb4.setText(hMap.get("op4"));
rg.setOnCheckedChangeListener(this);
rb1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
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) {
}
}
下面是我如何将值从另一个类放入自定义适配器中。
String questions[] = { "what is your name", "why are you here" };
String op1[] = { "Anil", "Play" };
String op2[] = { "John", "Sick" };
String op3[] = { "David", "Write" };
String op4[] = { "Akshay", "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);