android – 如何在列表视图中选择多个radiobuttons并使用它们的位置安排在arraylist中

android – 如何在列表视图中选择多个radiobuttons并使用它们的位置安排在arraylist中,第1张

概述Iam有25个问题,每个都有4个选项(只有4个单选按钮)在listview中的四个中选择.我准确地获得选定的位置和他们的无线电ID但是我无法在arraylist中保持他们的位置与无线电id.还有一个问题当滚动列表视图时,检查的单选按钮放错了地方.请在下面帮助我. 提前致谢! 这是我的适配器类,请查看它. 公共类CustomListAdapter扩展BaseAdapter { private Int Iam有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",""                                                        + 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",""                                                        + 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类作为字段我正在创建一个类型为bean的arrayList
2.在onCreate中我正在创建对象并设置requID问题并将ADD设置为List
//这里是适配器代码

@OverrIDepublic VIEw getVIEw(int position,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类

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>();
总结

以上是内存溢出为你收集整理的android – 如何在列表视图中选择多个radiobuttons并使用它们的位置安排在arraylist中全部内容,希望文章能够帮你解决android – 如何在列表视图中选择多个radiobuttons并使用它们的位置安排在arraylist中所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1125378.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存