1

我正在尝试编写一个简单的清单应用程序,其中包含一个 LinkedList 视图,该视图具有文本和两个指示是/否的单选按钮。问题是,当我向下滚动时选中一个单选按钮时,LinkedList 会更改单选按钮中的答案。我很感激你有任何建议。

我的代码如下

我的主要布局

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:cacheColorHint="#00000000"
        android:divider="#b5b5b5"
        android:dividerHeight="1dp" />

</RelativeLayout>

我的行项目布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <RadioGroup
        android:id="@+id/radio_group1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical|center_horizontal"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/mRadio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="radio1" />

        <RadioButton
            android:id="@+id/mRadio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="radio2" />
    </RadioGroup>

</LinearLayout>

我的主要清单活动的后端 java 类

        public class Checklist2 extends Activity {

    //Class Values

    AlertHandler ah; // object that manages the database of alerts
    QuestionHandler qh; // object that manages the database of questions
    List <Question>questions ; //List that holds all the Question items in the program
    List <Alert> alerts; //List that holds all the Alert items in the program
    private ArrayList<RowObject> mSource;
    TextView mCountTextView; // from internet example
    ListView mListView;
     RadioGroupAdapter adapter;


     private ListView listView1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.checklist2);


            Log.w("Test", "setList()");
            setList();
            RadioGroup rg = (RadioGroup) findViewById(R.id.radio_group1);




        }

        //sets the ListView with questions
        private void setList(){
            mSource = new ArrayList<RowObject>();
            Log.w("Test", "ArrayList<RowObject> created");
            for (int i =0; i<questions.size(); i++){
            Question q = questions.get(i);
            mSource.add(new RowObject());
            mSource.get(i).setID(q.getID());
            mSource.get(i).setQuestion(q.getQuestion());
            mSource.get(i).setYes(false);
            mSource.get(i).setNo(false);
        }
        Log.w("Test", "array list filled");
        mListView = (ListView) findViewById(R.id.list);
        adapter = new RadioGroupAdapter(this, 
                 R.layout.listitem, mSource);
        Log.w("Test", "adapter created");
        mListView.setAdapter(adapter);
        Log.w("Test", "adapter set");
    }}

My RowObject.java class

package com.randstad.jpmcchecklist;

public class RowObject {
private int ID;
private boolean yes;
private boolean no;
 public String question;
    public RowObject(){
        super();
    }

    public RowObject(int iD, String q,boolean y) {
        super();
        ID = iD;
        question = q;
        this.yes = y;
    }

    public RowObject( String question) {
        super();
        this.question = question;
    }

    public void setYes(boolean y){
        yes = y;
    }
    public boolean getYes(){
        return yes;
    }
    public void setNo(boolean n){
        no = n;
    }
    public boolean getNo(){
        return no;
    }
    public void setQuestion(String q){
        question = q;
    }
    public String getQuestion(){
        return question;
    }
    public void setID(int id){
        ID = id;
    }
    public int getID(){
        return ID;
    }


}


My Adapter class       

public class RadioGroupAdapter extends ArrayAdapter<RowObject> {

Context context;
int layoutResourceId;
ArrayList <RowObject> data = null;
int position;
ViewHolder holder;
public RadioGroupAdapter(Context context, int layoutResourceId,
        ArrayList <RowObject> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    View row = convertView;
    holder = null;
    this.position = position;
    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new ViewHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.textView1);
        holder.yes = (RadioButton) row.findViewById(R.id.mRadio1);
        holder.no = (RadioButton) row.findViewById(R.id.mRadio2);
        holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
        row.setTag(holder);
        //final RadioButton[] rb = new RadioButton[2];
        /*
        for(int i=0; i<2; i++){
            rb[i]  = new RadioButton(context);
            //rb[i].setButtonDrawable(R.drawable.single_radio_chice);
            rb[i].setId(i);



            RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                    0, LayoutParams.WRAP_CONTENT);
            params.weight=1.0f;
            params.setMargins(15, 0, 5, 10);
            holder.group.addView(rb[i],params); //the RadioButtons are added to the radioGroup instead of the layout
        }
        rb[0].setText("Yes");
        rb[1].setText("No");
        row.setTag(holder);*/
    } else {
        holder = (ViewHolder) row.getTag();
    }

    holder.group.setOnCheckedChangeListener(new   RadioGroup.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            // TODO Auto-generated method stub
            switch(checkedId){
            case R.id.mRadio1:
                data.get(position).setYes(true);
                data.get(position).setNo(false);
                break;
            case R.id.mRadio2:
                data.get(position).setNo(true);
                data.get(position).setYes(false);
            }

            if(data.get(position).getYes()==true){
                holder.yes.setChecked(true);
                holder.no.setChecked(false);
            }else if(data.get(position).getNo() == true){
                holder.yes.setChecked(false);
                holder.no.setChecked(true);
            }else{
                holder.yes.setChecked(false);
                holder.yes.setChecked(false);
            }
        }
    });

    if(data.get(position).getYes()==true){
        holder.yes.setChecked(true);
        holder.no.setChecked(false);
    }else if(data.get(position).getNo() == true){
        holder.yes.setChecked(false);
        holder.no.setChecked(true);
    }else{
        holder.yes.setChecked(false);
        holder.yes.setChecked(false);
    }


    //RowObject option = data[position];
    //holder.txtTitle.setText(option.title);
    return row;
}
static class ViewHolder {
    TextView txtTitle;
    RadioGroup group;
    RadioButton yes;
    RadioButton no;
    int position;
}}

谢谢,我真的很感谢你的帮助。

4

2 回答 2

1

我猜错误在

else{
    holder.yes.setChecked(false);
    holder.yes.setChecked(false);
}

应该是哪个

else {
    holder.yes.setChecked(false);
    holder.no.setChecked(false);
}

并且为了性能 - 在 getView() 中调用 data.get(position) 一次并将其存储为局部变量。

于 2013-06-25T15:49:44.180 回答
0

试着row.setTag(holder);在之前打电话return row

此外,holder.yes.setChecked(false)在 else 情况下你有两次。

于 2013-06-25T15:51:54.040 回答