在Android中的动态列表视图中获取Radiogroup

时间:2016-01-12 09:15:21

标签: android listview

我们正在开发一个项目,其中有一个列表视图,每个列表项中都有一个radiogroup。单选按钮是动态生成的。列表视图底部有一个按钮,在按下它时我应该在每个广播组中获取所选的单选按钮值。以下是我的尝试。

按钮点击代码:

submit.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
               for (m = 0; m < question_list_array.size(); m++)
               {
                       View view = question_list.getChildAt(m);
                       RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.single_radiogroup);//try to get radioGrop at that item
                       radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
                           @Override
                           public void onCheckedChanged(RadioGroup group, int checkedId) {
                               for (int n = 0; n < group.getChildCount(); n++) {
                                   RadioButton rb = (RadioButton) group.getChildAt(n);
                                   if (rb.getId() == checkedId) {
                                       String result = rb.getText() + "";
                                       Toast.makeText(MainActivity.this, result, Toast.LENGTH_LONG).show();
                                       return;
                                   }
                               }
                           }
                       });
               }
           }
       });

适配器代码:

public class QuestionAdapter extends BaseAdapter {
    ArrayList<QuestionsDataModel>  question_list_array;
    List<OptionsDataModel> option_list;
    Context context;
    LayoutInflater inflater;
    SingleHolder sh;
    int k=0,l=0;
    View v_single=null,v_multiple=null,v_final=null;
    MultipleHolder mh;
    int number_of_options;
    int position_counter=0;
    public  static  final  int TYPE_HEADER=0;
    public  static  final  int TYPE_ROW=1;
    public QuestionAdapter(ArrayList<QuestionsDataModel> question_list_array, Context context) {
        this.question_list_array=question_list_array;
        this.context=context;
        inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    }

    @Override
    public int getCount() {
        return question_list_array.size();
    }

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

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

   @Override
    public int getViewTypeCount()
    {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return (question_list_array.get(position).getQuestion_type().equals("single"))?TYPE_HEADER :TYPE_ROW;
    }

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

        option_list = question_list_array.get(position).option_list;
        number_of_options = option_list.size();


            v_single=convertView;
            if (v_single == null)
            {
                v_single = inflater.inflate(R.layout.single_choice_layout, null);
                sh = new SingleHolder();
                sh.single_question_name = (TextView) v_single.findViewById(R.id.single_question_name);
                sh.single_radiogroup = (RadioGroup) v_single.findViewById(R.id.single_radiogroup);
                v_single.setTag(sh);
            }
            else
            {
                sh = (SingleHolder) v_single.getTag();
            }
            sh.single_radiogroup.clearCheck();
            sh.single_radiogroup.removeAllViews();
            sh.single_question_name.setText(question_list_array.get(position).getQuestion_name());
           for (int j = 0; j < number_of_options; j++)
            {
                RadioButton rb = new RadioButton(context);
                rb.setText(option_list.get(j).getOption_name());
                rb.setFocusable(false);
                sh.single_radiogroup.addView(rb);
            }

           v_final=v_single;

        return v_final;
    }

    private   class SingleHolder
    {
        TextView single_question_name;
        RadioGroup single_radiogroup;
    }
}

问题是我在单击按钮时遇到NullPointerException,试图从列表中的视图对象获取radiogroup。以下代码是我获得NPE的地方。

View view = question_list.getChildAt(m);
                       RadioGroup radioGroup = (RadioGroup) view.findViewById(R.id.single_radiogroup);

0 个答案:

没有答案