android - 带有单选组错误的 ListView

标签 android android-layout

我正在创建一个自定义 ListView ,动态单选按钮添加到单选组直到我得到我想要的但是当我尝试在第一行中选择一个单选按钮然后自动选择第 5、9、13 行中的第一个按钮当我选择第二行中的任何按钮时,将选择第 6、8、12 行中的相同按钮,我在这里做错了什么以及我的适配器类

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;

}

}

我发布了我的工作代码,这样它会对其他人有所帮助

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;

}

最佳答案

那是因为 View 被重用了。你必须在 if(convertView == null) 的 else 中设置 holder.mgroup

我对你的代码做了一些改动:

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;

}

我的 imageView 实现(剥离):

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;
}

关于android - 带有单选组错误的 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8588827/

相关文章:

java - wrap_content 或 match_parent 未给出正确的结果

java - 如何在从 Android 服务类启动其他 Activity 后关闭 1 个 Activity

android - 复合布局是否总是需要自己的类?

java - 设置 Cordova 的 JAVA_HOME 路径

android - 在 Android ConstraintLayout 中创建具有可变数量 n 的网格 (n × n)

android - 打瞌睡时在后台运行应用程序

java - Android - createScaledBitmap 问题

java - Android - 在 TableRow 上设置固定高度似乎不起作用

android - Android 上的 Robolectic 在哪里保存测试运行期间创建的文件?

java - 为什么我的 TabListener 不起作用?