0

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


    }
}
4

2 回答 2

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

希望能帮助到你。

于 2014-10-21T11:39:37.210 回答
0

试试这个方法,希望这能帮助你解决你的问题。

取一个HashMap,其中包含Interger作为键和String作为值,并尝试将所有四个RadioButton id 作为键和RadioButton文本作为值,并尝试将此HashMap设置为RadioGroup。现在当任何RadioButton Check Change Listener 调用从RadioGroup Tag获取HashMap并获取使用选中的RadioButton id从HashMap检查RadioButton文本。

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;
    }
}
于 2014-10-21T12:02:08.063 回答