2

everybody i am creating a custom listview with dynamic radiobuttons adding to radiogroup upto that i am getting what i want but when i try to select one radio button in first row then automatically first button in 5th ,9th,13 row is getting selected and when i select any button in second row then same button in 6th,8th,12th row is getting selected what i am doing wrong here and my adapter class

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
String tag = "Events";
private final List<Model> list;
private final Activity context;

public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox,checkbox1;
    protected RadioGroup mgroup;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.d(tag," 3");
    View view = null;
    if (convertView == null) {
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        //viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        //viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
        viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);
        final RadioButton[] mbutton=new RadioButton[5];
           for(int l=0;l<5;l++){
               mbutton[l]=new RadioButton(context);
               mbutton[l].setText("test"+l);


               viewHolder.mgroup.addView(mbutton[l]);

           }
           viewHolder.mgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
               public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
                    for(int i=0; i<mRadioGroup.getChildCount(); i++) {
                         RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
                         //int t=table.indexOfChild(table_row);
                         //System.out.println(t);
                         int t=mRadioGroup.getId();
                         System.out.println(t);

                         if(btn.getId() == checkedId) {
                              String text = btn.getText().toString();
                              // do something with text
                              Log.d(text," event1");
                              return;
                         }
                    }
               }
          });


        view.setTag(viewHolder);
        Log.d(tag,"me");

    } else {
        view = convertView;
        Log.d(tag,"meeee");

    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());

    Log.d(tag," event7");

    return view;

}

}

i am posting my working code so that it will be helpfull to others have a look at it

public class InteractiveArrayAdapter extends ArrayAdapter<Model> implements OnClickListener {
String tag = "Events";
private final List<Model> list;
private final Activity context;
int li,jh;



public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context;
    this.list = list;
}

 static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox,checkbox1;
    protected RadioGroup mgroup;
    protected RadioButton mbutton;
}



@Override
public View getView( final int position, View convertView, ViewGroup parent) {
    //Log.d(tag," 3");

     View view =null;
    if (convertView == null) {
        //System.out.println("ok");
        LayoutInflater inflator = context.getLayoutInflater();
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        final ViewHolder viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);

        viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);


        final RadioButton[] mbutton=new RadioButton[5];
           for(int l=0;l<5;l++){
               mbutton[l]=new RadioButton(context);
               mbutton[l].setText("test"+l+position);
               mbutton[l].setId(l+1);
               mbutton[l].setOnClickListener(new View.OnClickListener(){

                    @Override
                    public void onClick(View v) {
                        // TODO Auto-generated method stub
                        RadioButton byt=(RadioButton) v;
                        byt.isChecked();
                        byt.getId();
                Model element = (Model) viewHolder.mgroup.getTag();
                        element.setBte(byt.getId());
                            element.gte((String) viewHolder.text.getText());
                        System.out.println("givemeresult");
                    }

                   });


               viewHolder.mgroup.addView(mbutton[l]);

               }
         view.setTag(viewHolder);
           viewHolder.mgroup.setTag(list.get(position));


    } else {
        view = convertView;
        ((ViewHolder) view.getTag()).mgroup.setTag(list.get(position));
        }
     ViewHolder holder = (ViewHolder) view.getTag();
     view.getTag()).mgroup.getTag();
     holder.text.setText(list.get(position).getName());


    if (list.get(position).getName()==list.get(position).done()){

        holder.mgroup.check(list.get(position).isSelected());
    }
    else {

        holder.mgroup.clearCheck();
    }




    return view;

}
Ghouse
  • 516
  • 1
  • 8
  • 29

1 Answers1

2

That's because views are reused. You have to set holder.mgroup in the else of if(convertView == null)

Your code a bit changed by me:

public class InteractiveArrayAdapter extends ArrayAdapter<Model> {
String tag = "Events";
private final List<Model> list;
private final Activity context;

public InteractiveArrayAdapter(Activity context, List<Model> list) {
    super(context, R.layout.rowbuttonlayout, list);
    this.context = context.getApplicationContext();
    this.list = list;
}

static class ViewHolder {
    protected TextView text;
    protected CheckBox checkbox,checkbox1;
    protected RadioGroup mgroup;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    Log.d(tag," 3");
    View view = convertView;
    ViewHolder viewHolder = null;
    final RadioButton[] mbutton = null;
    if (view == null) {
        LayoutInflater inflator = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflator.inflate(R.layout.rowbuttonlayout, null);
        viewHolder = new ViewHolder();
        viewHolder.text = (TextView) view.findViewById(R.id.label);
        //viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
        //viewHolder.checkbox1 = (CheckBox) view.findViewById(R.id.checkbox1);
        viewHolder.mgroup = (RadioGroup) view.findViewById(R.id.radioGroup1);

        // I'm not sure about this..
        // Begin
        mbutton=new RadioButton[5];
       for(int l=0;l<5;l++){
           mbutton[l]=new RadioButton(context);
           mbutton[l].setText("test"+l);


           viewHolder.mgroup.addView(mbutton[l]);

        }

           viewHolder.mgroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
               public void onCheckedChanged(RadioGroup mRadioGroup, int checkedId) {
                    for(int i=0; i<mRadioGroup.getChildCount(); i++) {
                         RadioButton btn = (RadioButton) mRadioGroup.getChildAt(i);
                         //int t=table.indexOfChild(table_row);
                         //System.out.println(t);
                         int t=mRadioGroup.getId();
                         System.out.println(t);

                         if(btn.getId() == checkedId) {
                              String text = btn.getText().toString();
                              // do something with text
                              Log.d(text," event1");
                              return;
                         }
                    }
               }
          });
         // End 

        view.setTag(viewHolder);
        Log.d(tag,"me");

    } else {
        viewHolder = (ViewHolder) convertView.getTag();

        // Begin
        mbutton = viewHolder.mgroup;
        // End

        Log.d(tag,"meeee");

    }
    ViewHolder holder = (ViewHolder) view.getTag();
    holder.text.setText(list.get(position).getName());

    Log.d(tag," event7");

    return view;

}

}

My implementation of an imageView (stripped):

public class DealAdapter extends ArrayAdapter<DealObject> {

private Context mContext;
private Activity mActivity;
private ArrayList<DealObject> mItems;
private int mXmlId;

public DealAdapter(Context context, int textViewResourceId, ArrayList<DealObject> items, Activity activity) {
    super(context, textViewResourceId, items);
    this.mContext = context.getApplicationContext();
    this.mActivity = activity;
    this.mItems = items;
    this.mXmlId = textViewResourceId;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    //View v = null;
    View v = convertView;
    ViewHolder holder = null;
    RemoteImageLoader imageLoader = new RemoteImageLoader(mContext, true);
    RemoteImageView dealImage = null;

    DealObject mo = mItems.get(position); 
    try { 
        if (v == null) { 
            LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v = vi.inflate(mXmlId, null);
            holder = new ViewHolder();

            dealImage = (RemoteImageView) v.findViewById(R.id.deal_image);

            holder.dealImage = dealImage;
            v.setTag(holder);
        } else {
            // Get the ViewHolder back to get fast access to the ImageView.
            holder = (ViewHolder) convertView.getTag();
            holder.dealImage.setBackgroundColor(Color.WHITE);
            dealImage = holder.dealImage;
        }

        if(mo.getImage() != null){ 
            // calling reset is important to prevent old images from displaying in a recycled view.
            dealImage.reset();
            holder.dealImage.setImageUrl(imageUrl);
            try {
                holder.dealImage.loadImage();
            }
            catch (Exception ex) {
            }
        }  
    }
    catch (Exception ex) { 
        ex.printStackTrace();
    }
    return v; 
} 

private static final class ViewHolder {
    private RemoteImageView dealImage;
}

}

Dante
  • 1,104
  • 1
  • 10
  • 15
  • sorry i did not get you would you explain it .because i am a newbie to this platform – Ghouse Dec 21 '11 at 11:47
  • Hi, please anybody just let me know about that set holder.mgroup answer – Ghouse Dec 21 '11 at 12:17
  • Sure. Views are recycled in a listview. Please check this article for better understanding http://mobile.cs.fsu.edu/?p=527 . You should set the radiobutton.setChecked(true | false) in the else {} of "if(convertView == null)" – Dante Dec 21 '11 at 12:24
  • thanks for the link i am working on it hope it will solve my problem – Ghouse Dec 21 '11 at 14:08
  • No problem, it will be solved when you adapt it correctly like the article says. For the first 4 rows in the listview, convertView will be null. For the next rows, convertView will not be null and will reuse the first 4. (I think your screen shows 4 rows at a time, right?). If you want to have unique parts per row, you should set that in the else {}, because convertView is not null. – Dante Dec 21 '11 at 14:38
  • hai,Dante i have tried by setting the radiogroup in else part of "if(convertView == null)" but no success till now – Ghouse Dec 27 '11 at 06:40
  • I'm not sure about your radiobutton implementation. I can show you an implementation of an image instead of a radiobutton. – Dante Dec 27 '11 at 10:18
  • If you read the second coding part, you'll see that I reset the image. maybe you'll have to do something similar for the radiobutton. – Dante Dec 27 '11 at 10:31
  • hai dante you have made some modification in my code in that else part//Begin mbutton = viewHolder.mgroup; // End is giving error as cannot convert from RadioGroup to RadioButton[] – Ghouse Dec 30 '11 at 13:02
  • another thing i have observed in my code is that id of radiobuttons of first row and id of radiobuttons of fourth row are same when i try to set id to each button using random function then i am able to select all radiobuttons of a single group which is quite opposite to the behaviour of radiogroup. – Ghouse Dec 30 '11 at 13:14
  • Yes I know, I do not know how to deal with the radio buttons by heart. Try to get it working first with a checkbox or an image. Those views will also be recycled and show same behaviour. Then swap the checkbox or imageview with the radiobuttons. – Dante Dec 30 '11 at 14:14
  • hai Dante any how thanks for your help is there any way that when any radiobutton is selected then to get textview associated with it any suggestion will be a great help once again thanks a lot. – Ghouse Dec 31 '11 at 05:19
  • hai Dante code is working properly now only some error is there. – Ghouse Dec 31 '11 at 09:35