0

I am currently creating a list view with multiple radio buttons, but when I set the radio button value and scroll the list view, then its state changes. Means some random radio buttons are selected.My Adapter code is,

RadioGroupAdapter.java

public RadioGroupAdapter(Context context, int layoutResourceId,
            Option[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        MatrixHolder holder = null;



        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MatrixHolder();
            holder.txtTitle = (TextView) row.findViewById(R.id.heading);
            holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
            final RadioButton[] rb = new RadioButton[2];
            for(int i=0; i<2; i++){

                rb[i]  = new RadioButton(context);
               // rb[i].setButtonDrawable(R.drawable.single_radio_chice);
                rb[i].setId(i);
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT);
                params.weight=1.0f;
                params.setMargins(5, 0, 5, 10);
                holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
            }


           // ((MatrixHolder)holder).group.clearCheck();


            row.setTag(holder);
        } else {
            holder = (MatrixHolder) row.getTag();
        }

        Option option = data[position];
        holder.txtTitle.setText(option.title);
        return row;
    }

Please give me some solutions or let me know how to solve this. I am working on it from last 4 days,and not getting it. Let me know how to set state. Suggest or do edit my code

Ashish Patil
  • 471
  • 1
  • 9
  • 24
  • 1
    this may help you visit: http://stackoverflow.com/questions/8771083/radiobutton-state-updation-after-filtering-in-custom-listview – Rajesh Sep 17 '13 at 10:38
  • 1
    You have to implement a sparse boolean array and yes a custom adapter with a holder pattern in getView() will help a lot. – Skynet Sep 17 '13 at 10:44
  • @Pirate Cube : Can you please let me know how to do it or code ? – Ashish Patil Sep 17 '13 at 10:47
  • Check this out, try the first five suggested questions on the right side too http://stackoverflow.com/questions/4590856/how-to-get-selected-items-from-multi-select-list-view – Skynet Sep 17 '13 at 10:49
  • nothing happening brother. Please suggest me some change in code. – Ashish Patil Sep 17 '13 at 10:58
  • anybody have some idea regardng this ? – Ashish Patil Sep 17 '13 at 11:54
  • Check this out:http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works/14108676#14108676. This might help as well: http://stackoverflow.com/questions/18589716/erratic-behaviour-of-listview-android – Skynet Sep 17 '13 at 12:53
  • You might want to go through this too, extensive knowledge!! http://www.youtube.com/watch?v=wDBM6wVEO70 – Skynet Sep 17 '13 at 13:03
  • I dont how this is working but It is. http://stackoverflow.com/a/19092520/2080965 – Lokesh Tiwari Feb 12 '15 at 17:29

0 Answers0