listview with radio group error

后端 未结 1 1027
星月不相逢
星月不相逢 2020-12-07 04:33

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 firs

1条回答
  •  生来不讨喜
    2020-12-07 05:23

    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 {
    String tag = "Events";
    private final List list;
    private final Activity context;
    
    public InteractiveArrayAdapter(Activity context, List 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

    }

    My implementation of an imageView (stripped):

    public class DealAdapter extends ArrayAdapter {
    
    private Context mContext;
    private Activity mActivity;
    private ArrayList mItems;
    private int mXmlId;
    
    public DealAdapter(Context context, int textViewResourceId, ArrayList 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;
    }
    

    }

    0 讨论(0)
提交回复
热议问题