自定义列表视图与无线电组滚动问题

时间:2016-01-06 09:25:36

标签: android listview

我正在尝试实现包含单选按钮的自定义列表视图,但是当我滚动列表视图时,所选的单选按钮会自动取消选择。我可以解决但我无法弄清楚确切的问题。我是新来的。 我的适配器类代码是

 public class CustomAdapterListView extends ArrayAdapter<FDetailsPOJO> {

    private final List<FDetailsPOJO> list;
    private final Activity context;
    int resource;
    ViewHolder viewHolder = null;
    private LayoutInflater mLayoutInflater;

    public CustomAdapterListView(Activity context, int resource, List<FDetailsPOJO> objects) {
        super(context, resource, objects);
        this.context = context;
        this.resource = resource;
        this.list = objects;
        mLayoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }


    public View getView(final int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            try {

                convertView = mLayoutInflater.inflate(R.layout.custom_checkbox_row, parent, false);

                viewHolder = new ViewHolder();

                viewHolder.tvName = (TextView) convertView.findViewById(R.id.custom_checkbox_row_tv);
                viewHolder.radioGroup = (RadioGroup) convertView.findViewById(R.id.fragmenttab2_options);

                viewHolder.rbYes = (RadioButton) convertView.findViewById(R.id.fragmenttab2_options_yes);
                viewHolder.rbNo = (RadioButton) convertView.findViewById(R.id.fragmenttab2_options_no);

                viewHolder.radioGroup.setTag(position);
                convertView.setTag(R.id.custom_checkbox_row_tv, viewHolder.tvName);
                convertView.setTag(R.id.fragmenttab2_options, viewHolder.radioGroup);
                convertView.setTag(viewHolder);
            } catch (Exception e) {
                e.printStackTrace();
            }
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        viewHolder.radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                int isChecked = (int) group.getTag();
                try {

                    switch (checkedId) {
                        case R.id.fragmenttab2_options_yes:
                            try {
                                list.get(isChecked).setCheckedYes(true);
                                list.get(isChecked).setCheckedNo(false);
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                            break;
                        case R.id.fragmenttab2_options_no:
                            try {
                                list.get(isChecked).setCheckedNo(true);
                                list.get(isChecked).setCheckedYes(false);

                            } catch (Exception e) {
                                e.printStackTrace();
                            }

                            break;
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        });
        try {
            viewHolder.radioGroup.setTag(position);
            viewHolder.tvName.setText(list.get(position).getFitnessItem());
            viewHolder.rbYes.setChecked(list.get(position).isCheckedYes());
            viewHolder.rbNo.setChecked(list.get(position).isCheckedNo());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return convertView;
    }

    private class ViewHolder {
        TextView tvName;
        RadioGroup radioGroup;
        RadioButton rbYes;
        RadioButton rbNo;
    }
}

在搜索解决方案时,我找到了一个解决问题的代码片段,但这不是同一链接中提到的适当方式

 @Override
public int getViewTypeCount() {
    // TODO Auto-generated method stub
    return list.size();
}

@Override
public int getItemViewType(int position) {
    // TODO Auto-generated method stub
    return position;
}

2 个答案:

答案 0 :(得分:1)

您必须在单独的变量中跟踪选定的无线电选项,并根据列表的getView方法中的值手动选择标记。

答案 1 :(得分:0)

您应该使用List或Map来记录适配器中每个单选按钮的状态。当调用onCheckedChanged方法时,将当前单选按钮的状态保存到List或Map中,并根据getView方法中的List或Map设置每个单选按钮的检查状态。