如何 Select listview 中的多个单选按钮并使用它们的位置在 arraylist 中排列

how to Select multiple radiobuttons in listview and arranging in arraylist using their positions

我有 25 个问题,每个问题有 4 个选项(只有 4 个单选按钮)在 listview.Iam 中的四个选项中选择,准确地获得选定的位置和他们的无线电 ID,但我无法用无线电 ID 保持他们的位置在 arraylist.And 中,还有一个问题是当滚动列表视图时,检查的单选按钮是下面的 misplaced.Please 并帮助我。

提前致谢!

这是我的适配器class,请查看。

public class CustomListAdapter 扩展了 BaseAdapter {

    private Integer[] listData;
    int selectedPosition = -1;
    private LayoutInflater mInflater;

    public CustomListAdapter(Context context, Integer[] listData) {
        super();
        this.listData = listData;
        mInflater = LayoutInflater.from(context);
        validdata = new ArrayList<String>();
        int size = values.length;
        Log.d("size", "" + size);
        for (int i = 0; i < size; i++) {
            checked.add(false);
            validdata.add("");
            checkeddata.add("");

        }
    }

    @Override
    public int getCount() {
        return listData.length;
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    public View getView(final int position, View convertView,
            ViewGroup parent) {
        final ViewHolder holder;
        View view = convertView;

        if (view == null) {
            view = mInflater.inflate(R.layout.items, null);
            holder = new ViewHolder();
            holder.headlineView = (TextView) view
                    .findViewById(R.id.textview1);
            holder.r1 = (RadioButton) view.findViewById(R.id.radio0);
            holder.r2 = (RadioButton) view.findViewById(R.id.radio1);
            holder.r3 = (RadioButton) view.findViewById(R.id.radio2);
            holder.r4 = (RadioButton) view.findViewById(R.id.radio3);
            holder.rp = (RadioGroup) view.findViewById(R.id.rgOpinion);
            holder.rp.setTag(position);
            view.setTag(holder);

            holder.rp
                    .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                        public void onCheckedChanged(RadioGroup group,
                                int checkedId) {
                            // TODO Auto-generated method stub
                            View radioButton = group
                                    .findViewById(checkedId);
                            final String radioId = String.valueOf(group
                                    .indexOfChild(radioButton));
                            final int radio = group
                                    .indexOfChild(radioButton);
                            String radioid;

                            Log.d("radioid", "" + radioId);
                            if (holder.r1.isChecked()) {
                                holder.r1
                                        .setChecked(position == selectedPosition);
                                holder.r1.setTag(position);
                                holder.r1.setChecked(true);
                                holder.r1
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                selectedPosition = (Integer) view
                                                        .getTag();
                                                notifyDataSetChanged();
                                                Log.d("selectesxsxad", ""
                                                        + selectedPosition);
                                                String s = listData[selectedPosition]
                                                        .toString();
                                                checked.add(
                                                        selectedPosition,
                                                        true);

                                                Log.d("valid", "" + checked);

                                            }
                                        });
                                radioid = "A";

                            } else if (holder.r2.isChecked()) {
                                holder.r2
                                        .setChecked(position == selectedPosition);
                                holder.r2.setTag(position);
                                holder.r2.setChecked(true);
                                holder.r2
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                selectedPosition = (Integer) view
                                                        .getTag();
                                                notifyDataSetChanged();
                                                Log.d("selectesxsxad", ""
                                                        + selectedPosition);
                                                validdata.add(
                                                        selectedPosition,
                                                        radioId);
                                                Log.d("valid", ""
                                                        + validdata);

                                            }
                                        });
                                radioid = "B";

                            } else if (holder.r3.isChecked()) {
                                holder.r3
                                        .setChecked(position == selectedPosition);
                                holder.r3.setTag(position);

                                holder.r3.setChecked(true);
                                holder.r3
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                selectedPosition = (Integer) view
                                                        .getTag();
                                                notifyDataSetChanged();
                                                Log.d("selectesxsxad", ""
                                                        + selectedPosition);
                                                validdata.add(
                                                        selectedPosition,
                                                        radioId);
                                                Log.d("valid", ""
                                                        + validdata);

                                            }
                                        });
                                radioid = "C";

                            } else if (holder.r4.isChecked()) {
                                holder.r4
                                        .setChecked(position == selectedPosition);
                                holder.r4.setTag(position);
                                holder.r4.setChecked(true);
                                holder.r4
                                        .setOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                selectedPosition = (Integer) view
                                                        .getTag();
                                                notifyDataSetChanged();
                                                Log.d("selectesxsxad", ""
                                                        + selectedPosition);
                                                validdata.add(
                                                        selectedPosition,
                                                        radioId);
                                                Log.d("valid", ""
                                                        + validdata);

                                            }
                                        });
                                radioid = "D";

                            }
                        }
                    });

        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.rp = (RadioGroup) view.findViewById(R.id.rgOpinion);
        holder.r1 = (RadioButton) view.findViewById(R.id.radio0);
        holder.r2 = (RadioButton) view.findViewById(R.id.radio1);
        holder.r3 = (RadioButton) view.findViewById(R.id.radio2);
        holder.r4 = (RadioButton) view.findViewById(R.id.radio3);
        holder.headlineView.setText(listData[position].toString());
        holder.r1.getTag(position);
        if (holder.r1.isChecked()) {
            holder.r1.setChecked(true);
        } else if (holder.r2.isChecked()) {
            holder.r2.setChecked(true);
        } else if (holder.r3.isChecked()) {
            holder.r3.setChecked(true);
        } else if (holder.r4.isChecked()) {
            holder.r4.setChecked(true);
        }
        return view;
    }

    public class ViewHolder {
        TextView headlineView;
        RadioGroup rp;
        RadioButton r1, r2, r3, r4;
    }

尝试使用此代码,它可以正常工作 1.i 取了一个 bean class 以字符串和整数作为字段 我正在创建一个类型为 bean 的数组列表 2. 在 onCreate 中我正在创建对象并设置 requid 问题并添加到列表 // 这里是适配器代码

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    ViewHolder mHolder;
    // if (convertView == null) {
    mHolder = new ViewHolder();
    convertView = LayoutInflater.from(mContext).inflate(R.layout.listitems, null);
    mHolder.mTextView = (TextView) convertView.findViewById(R.id.textView1);
    mHolder.mGroup = (RadioGroup) convertView.findViewById(R.id.radioGroup1);
    mHolder.mRedButton1 = (RadioButton) convertView.findViewById(R.id.radio0);
    mHolder.mRedButton2 = (RadioButton) convertView.findViewById(R.id.radio1);
    mHolder.mRedButton3 = (RadioButton) convertView.findViewById(R.id.radio2);
    // convertView.setTag(mHolder);
    // }
    // else {
    // mHolder = (ViewHolder) convertView.getTag();
    // }
    final MyQuestion mMyQuestion = myQuestions.get(position);
    mHolder.mTextView.setText(mMyQuestion.getmQuestion());

    int mQuestion_answed = mMyQuestion.getQuestion_answed();
    mHolder.mGroup.setOnCheckedChangeListener(null);

    if (mQuestion_answed != 0) {
        switch (mQuestion_answed) {
        case 1:
            mHolder.mRedButton1.setChecked(true);
            break;
        case 2:
            mHolder.mRedButton2.setChecked(true);
            break;
        case 3:
            mHolder.mRedButton3.setChecked(true);
            break;
        default:
            break;
        }
    }

    // listener for radioGroup
    mHolder.mGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            switch (checkedId) {
            case R.id.radio0:
                mMyQuestion.setQuestion_answed(1);
                break;
            case R.id.radio1:
                mMyQuestion.setQuestion_answed(2);
                break;
            case R.id.radio2:
                mMyQuestion.setQuestion_answed(3);
                break;
            default:
                break;
            }
        }
    });
    return convertView;
}

// 视图持有者

private class ViewHolder {
    TextView mTextView;
    RadioGroup mGroup;
    RadioButton mRedButton1, mRedButton2, mRedButton3;
}

//豆子class

public class MyQuestion {
private String mQuestion;
private int question_answed;

public MyQuestion() {
    super();
}

public String getmQuestion() {
    return mQuestion;
}

public void setmQuestion(String mQuestion) {
    this.mQuestion = mQuestion;
}

public int getQuestion_answed() {
    return question_answed;
}

public void setQuestion_answed(int question_answed) {
    this.question_answed = question_answed;
}

}

这是我正在使用的 ArrayList

private ArrayList<MyQuestion> mQuestionsList = new ArrayList<MyQuestion>();