滚动

时间:2015-06-08 11:38:03

标签: android listview android-listview radio-button dynamically-generated

我已经创建了动态单选按钮。它们显示得非常好,但是当我选择并滚动它时会丢失它的位置。我知道使用ViewHolder类设置和获取标记并转换视图的方式。但这一次,我有动态ID,所以我不能标记id。我试过这样的

    converview.setTag(rdbtn.getId(),rdbtn);

但它会抛出The key must be an application specific Resource id

这是我的自定义适配器类

      static class FeedBacksubmitionViewHolder {
    protected TextView Question;
    protected EditText Cooment;
    protected ViewGroup Radiogroup; 
    protected TextView Q;

}
public View getView(int position,View converview,ViewGroup parent) {
    FeedBacksubmitionViewHolder viewHolder = null;
    if (converview ==null ){
        LayoutInflater inflater=context.getLayoutInflater();
        converview=inflater.inflate(R.layout.listfeedbacksubmitionquestion, null,true);
        viewHolder = new FeedBacksubmitionViewHolder();

        viewHolder.Question = (TextView) converview.findViewById(R.id.tvFeedbacksubmitionquestion);
        viewHolder.Cooment = (EditText) converview.findViewById(R.id.edfeedbacksubmitioncomment);
        viewHolder.Radiogroup=((ViewGroup)converview. findViewById(R.id.radiogroup));
        viewHolder.Q =(TextView)converview.findViewById(R.id.feedba1);

        LinearLayout ll = new LinearLayout(context);
        ll.setOrientation(LinearLayout.VERTICAL);


        int number = feedBackSubmitionOptionList.size();

        //if(position!=feedBackSubmitionQuestionList.size()-1 || )
        for (int i = 0; i < number; i++) {
            //if(ll.getChildAt(0).getId()!=feedBackSubmitionOptionList.get(i).getId()){
            RadioButton rdbtn = new RadioButton(context);
            rdbtn.setId(feedBackSubmitionOptionList.get(i).getId());
            rdbtn.setText(feedBackSubmitionOptionList.get(i).getOptionLabel());
            ll.addView(rdbtn);
            converview.setTag(rdbtn.getId(),rdbtn);

            //}
        }





        converview.setTag(viewHolder);
        converview.setTag(R.id.tvFeedbacksubmitionquestion,viewHolder.Question);
        converview.setTag(R.id.edfeedbacksubmitioncomment,viewHolder.Cooment);
        converview.setTag(R.id.radiogroup,viewHolder.Radiogroup);
        converview.setTag(R.id.feedba1,viewHolder.Q);





        viewHolder.Radiogroup.addView(ll);


    }else{
        viewHolder= (FeedBacksubmitionViewHolder)converview.getTag();

    }

1 个答案:

答案 0 :(得分:1)

我最近几天在这个问题上挣扎,但做R&amp; N让问题得到了解决。 你可以做这样的事情。

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub// if(position==0)//Toast.makeText(context, ""+QuestionViewPageAdapter.mRadioGroupData, Toast.LENGTH_SHORT).show();

    final ViewHolder viewHolder;
    View row=convertView;

    radioButton=new RadioButton[Integer.parseInt(mNoOfCategoryGreading)]; LayoutInflater inflater =LayoutInflater.from(context);//(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        row = inflater.inflate(R.layout.question_row, parent, false);

        viewHolder=new ViewHolder();

        viewHolder.tv_qContent=(TextView)row.findViewById(R.id.tv_question);
        viewHolder.rg=(RadioGroup)row.findViewById(R.id.rgrp);

        for(int i=0;i<Integer.parseInt(mNoOfCategoryGreading);i++)
        {
            radioButton[i]=new RadioButton(context);
            radioButton[i].setLayoutParams(new RadioGroup.LayoutParams(0, LayoutParams.WRAP_CONTENT,1f));
            //1f for layout_weight="1"
            radioButton[i].setId(i);
            radioButton[i].setText(""+(i+1));

            viewHolder.rg.addView(radioButton[i]);
        }
        row.setTag(viewHolder);


        for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
            int p=Integer.parseInt(""+QuestionViewPageAdapter.mRadioGroupData.get(i).get("position"));
            String id=QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id");
            if(p!=-1 && id.equals(""+data.get(position).get(AppData.question_Set_category_QuestionID)))
            {//radioButton[p].setSelected(true);
                viewHolder.rg.check(p-1);}//Toast.makeText(context, "Yes "+p, Toast.LENGTH_LONG).show();}viewHolder.tv_qContent.setText((position+1)+". "+data.get(position).get(AppData.question_Set_category_QuestionContent));
    viewHolder.tv_qContent.setTypeface(typeface_muso_regular300);



    viewHolder.rg.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub

            String s=data.get(position).get(AppData.question_Set_category_QuestionID);
            isAvailabel(s);


                    int radioButtonID = viewHolder.rg.getCheckedRadioButtonId();
                    View radioButton = viewHolder.rg.findViewById(radioButtonID);
                    int idx = viewHolder.rg.indexOfChild(radioButton);
                    HashMap<String, String> map=new HashMap<String, String>();
                    map.put("position", ""+(idx+1));
                    map.put("Id", ""+data.get(position).get(AppData.question_Set_category_QuestionID));
                    map.put("CategoryId", ""+mCategoryId);
                    QuestionViewPageAdapter.mRadioGroupData.add(map);
            }


    });





    return row;
}

private void isAvailabel(String qId){

    for(int i=0;i<QuestionViewPageAdapter.mRadioGroupData.size();i++){
        if(qId.equals(QuestionViewPageAdapter.mRadioGroupData.get(i).get("Id"))){
            position=i;

            QuestionViewPageAdapter.mRadioGroupData.remove(i);
        }
    }

}

 class ViewHolder {
     TextView tv_qContent ,tv_1,tv_2,tv_3;
        RadioGroup rg;
        RadioButton rBtn1,rBtn2,rBtn3,rBtn4,rBtn5;
 }

我无法删除对您来说不重要的代码。你必须研究代码,我想你会得到这个想法。 如果我解决了您的问题,请进行投票。