如何在列表视图中选择多个单选按钮并使用其位置排列在arraylist中

时间:2015-01-05 09:29:57

标签: android listview radio-button

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

提前致谢!

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

公共类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 个答案:

答案 0 :(得分:1)

试试这个代码它正在运行 1.i已经使用一个带有字符串和整数的bean类作为字段我正在创建一个类型为bean的arraylist 2.在onCreate中我正在创建对象并设置requid问题并将ADD设置为list //这里是适配器代码

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

// VIewHolder

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

// bean 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>();