如何从Android中的Custom Listview获取单选按钮的值?

时间:2014-10-18 05:35:50

标签: android android-listview custom-adapter android-radiogroup android-radiobutton

这是一个自定义列表视图,通过扩展BaseAdapter。我需要获取所选单选按钮的值,但我不知道如何。所以请帮助我。 下面是Custom Adapter类。

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

3 个答案:

答案 0 :(得分:6)

试试这个:

  rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                @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) {

                            System.out.println(btn.getText().toString());


                    }

                }
            });

答案 1 :(得分:1)

这会帮助你.........

Context context;
String[] ques_no, ques_name, opt1, opt2, opt3, opt4,answer;
LayoutInflater inflater;
ArrayList<QuestionList>qArrayList;


public TestAdapter(Context context, ArrayList<QuestionList> testData)
{
    this.context=context;
    this.qArrayList=testData;

}

/*public TestAdapter(Context context, String[] ques_no, String[] ques_name,String[] opt1,String[] opt2,String[] opt3,String[] opt4) {
    this.context = context;
    this.ques_no = ques_no;
    this.ques_name = ques_name;
    this.opt1 = opt1;
    this.opt2 = opt2;
    this.opt3 = opt3;
    this.opt4 = opt4;

}*/



@Override
public int getCount() {
    if(qArrayList !=null){
       return qArrayList.size();
    }
    return ques_no.length;
}

@Override
public Object getItem(int position) {
    return null;
}

@Override
public long getItemId(int position) {
    return 0;
}

public View getView(int position, View convertView, ViewGroup parent) {


    ViewHolder holder = null;

    inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null) {
        convertView = inflater.inflate(R.layout.test_rowlayout, null);
        holder = new ViewHolder();
        holder.radio_group=(RadioGroup)convertView.findViewById(R.id.RadioGroup01);
        holder.question_no = (TextView) convertView.findViewById(R.id.question_no);
        holder.question_name = (TextView) convertView.findViewById(R.id.question_name);
        holder.option1 = (RadioButton) convertView.findViewById(R.id.option1);
        holder.option2 = (RadioButton) convertView.findViewById(R.id.option2);
        holder.option3 = (RadioButton) convertView.findViewById(R.id.option3);
        holder.option4 = (RadioButton) convertView.findViewById(R.id.option4);
        convertView.setTag(holder);

    } else {
        holder = (ViewHolder) convertView.getTag();
    }
    //holder.question_no.setText(ques_no[position]);
    holder.question_name.setText(""+qArrayList.get(position).Question);
    holder.option1.setText(""+qArrayList.get(position).Option1);
    holder.option2.setText(""+qArrayList.get(position).Option2);
    holder.option3.setText(""+qArrayList.get(position).Option3);
    holder.option4.setText(""+qArrayList.get(position).Option4);      


    HashMap<Integer,String> radioMap = new HashMap<Integer, String>();
    radioMap.put(holder.option1.getId(),holder.option1.getText().toString());
    radioMap.put(holder.option2.getId(),holder.option2.getText().toString());
    radioMap.put(holder.option3.getId(),holder.option3.getText().toString());
    radioMap.put(holder.option4.getId(),holder.option4.getText().toString());

    holder.radio_group.setTag(radioMap);
    holder.radio_group.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();


            int childCount = group.getChildCount();
            for (int x = 0; x < childCount; x++) {
                RadioButton btn = (RadioButton) group.getChildAt(x);

                if (btn.getId() == checkedId) {

                    System.out.println(btn.getText().toString());
        }
            }
        }
    });


    return convertView;
}

private class ViewHolder {
    ImageView img_fimage;
    TextView question_no, question_name;
    RadioGroup radio_group;
    RadioButton option1, option2, option3, option4,radio_btn_answer;
}

}

答案 2 :(得分:0)

使用以下代码。

@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
string item = hMap1.get(checkedId);  //for single item
int count= group.getChildCount();
                    for (int x = 0; x < count; x++) {
                        RadioButton btn = (RadioButton) group.getChildAt(x);

                        if (btn.getId() == checkedId) {

                           array.add(btn.gettext().tostring);    //adding values to array list
}
}