Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有无线电组错误的listview_Android_Android Layout - Fatal编程技术网

Android 带有无线电组错误的listview

Android 带有无线电组错误的listview,android,android-layout,Android,Android Layout,每个人,我正在创建一个自定义列表视图,其中动态单选按钮添加到radiogroup,直到我得到我想要的,但当我尝试在第一行选择一个单选按钮时,第5、第9、第13行的第一个按钮自动被选中,当我选择第二行的任何按钮时,第6、第8行的同一个按钮,第12行被选中我在这里做错了什么以及我的适配器类 public class InteractiveArrayAdapter extends ArrayAdapter<Model> { String tag = "Events"; private fi

每个人,我正在创建一个自定义列表视图,其中动态单选按钮添加到radiogroup,直到我得到我想要的,但当我尝试在第一行选择一个单选按钮时,第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;

}
公共类InteractiveArrayAdapter扩展了ArrayAdapter{
String tag=“事件”;
私人最终名单;
私人最终活动背景;
公共InteractiveArrayAdapter(活动上下文、列表){
super(上下文,R.layout.rowbuttonlayout,列表);
this.context=上下文;
this.list=列表;
}
静态类视窗夹{
受保护的文本查看文本;
受保护复选框,复选框1;
受保护的放射组经理组;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
日志d(标签“3”);
视图=空;
if(convertView==null){
LayoutInflater充气器=上下文。getLayoutInflater();
视图=充气机。充气(R.layout.rowbuttonlayout,null);
最终ViewHolder ViewHolder=新的ViewHolder();
viewHolder.text=(TextView)view.findViewById(R.id.label);
//viewHolder.checkbox=(checkbox)view.findViewById(R.id.check);
//viewHolder.checkbox1=(复选框)view.findViewById(R.id.checkbox1);
viewHolder.mgroup=(RadioGroup)view.findViewById(R.id.RadioGroup 1);
最终单选按钮[]mbutton=新单选按钮[5];

对于(int l=0;l这是因为视图被重用。必须在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;

}
公共类InteractiveArrayAdapter扩展了ArrayAdapter{
String tag=“事件”;
私人最终名单;
私人最终活动背景;
公共InteractiveArrayAdapter(活动上下文、列表){
super(上下文,R.layout.rowbuttonlayout,列表);
this.context=context.getApplicationContext();
this.list=列表;
}
静态类视窗夹{
受保护的文本查看文本;
受保护复选框,复选框1;
受保护的放射组经理组;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
日志d(标签“3”);
视图=转换视图;
ViewHolder ViewHolder=null;
最终单选按钮[]mbutton=null;
如果(视图==null){
LayoutFlater充气机=context.getSystemService(context.LAYOUT\u充气机\u服务);
视图=充气机。充气(R.layout.rowbuttonlayout,null);
viewHolder=新的viewHolder();
viewHolder.text=(TextView)view.findViewById(R.id.label);
//viewHolder.checkbox=(checkbox)view.findViewById(R.id.check);
//viewHolder.checkbox1=(复选框)view.findViewById(R.id.checkbox1);
viewHolder.mgroup=(RadioGroup)view.findViewById(R.id.RadioGroup 1);
//我对此不太确定。。
//开始
mbutton=新单选按钮[5];

对于(int l=0;l抱歉,我没听懂,你能解释一下吗。因为我是这个平台的新手。嗨,请任何人告诉我集合持有者的情况。mgroup answerSure。视图在列表视图中循环使用。请查看本文以便更好地理解。你应该在“if”的else{}中设置radiobutton.setChecked(true | false)(convertView==null)“感谢我正在处理的链接,希望它能解决我的问题。没问题,当你像文章所说的那样正确地调整它时,它会得到解决。对于listview中的前4行,convertView将为null。对于下一行,convertView将不为null,并将重用前4行。(我认为您的屏幕一次显示4行,对吗?)。如果您希望每行有唯一的部分,您应该在else{}中设置它,因为convertView不为null。
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;

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