3

我是一个初学者,正在尝试使用在列表视图中动态生成的单选组创建一个列表视图。但是当我滚动该列表视图时,第一个选择的单选按钮突然改变了他们的选择,并且其他单选按钮被选中。为什么会发生这种情况?如何创建列表,其中单选按钮的选择在滚动列表后不会改变?我正在使用自定义适配器进行列表视图。我的代码是,

MainActivity.java

    package com.list.radiogroup;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ListView;

public class MainActivity extends Activity {

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

            Option weather_data[] = new Option[]
                    {
                        new Option("Heading1"),
                        new Option("Heading12"),
                        new Option("Heading3"),
                        new Option("Heading4"),
                        new Option("Heading5"),
                        new Option("Heading6"),
                        new Option("Heading7"),
                        new Option("Heading8"),
                        new Option("Heading9"),
                        new Option("Heading10"),
                        new Option("Heading11"),
                        new Option("Heading13")          

                    };
            RadioGroupAdapter adapter = new RadioGroupAdapter(this, 
                            R.layout.listitem, weather_data);
                    listView1 = (ListView)findViewById(R.id.list);
                    listView1.setAdapter(adapter);
        }



        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            getMenuInflater().inflate(R.menu.activity_main, menu);
            return true;
        }

    }

RadioGroupAdapter.java

package com.list.radiogroup;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.LinearLayout.LayoutParams;

public class RadioGroupAdapter extends ArrayAdapter<Option> {

    Context context;
    int layoutResourceId;
    Option data[] = null;
    private List<Option> list;

    public RadioGroupAdapter(Context context, int layoutResourceId,
            Option[] data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = null;
        MatrixHolder holder = null;



        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);

            holder = new MatrixHolder(row);
            holder.txtTitle = (TextView) row.findViewById(R.id.heading);
            holder.group = (RadioGroup) row.findViewById(R.id.radio_group1);
            final RadioButton[] rb = new RadioButton[2];
            for(int i=0; i<2; i++)
            {
                rb[i]  = new RadioButton(context);
                rb[i].setId(i);
                if(!list.get(i).isChecked){
                    list.get(i).isChecked=false; // more implement here or may be this work
                    break;
                }
                RadioGroup.LayoutParams params = new RadioGroup.LayoutParams(
                        0, LayoutParams.WRAP_CONTENT);
                params.weight=1.0f;
                params.setMargins(5, 0, 5, 5);
                holder.group.addView(rb[i],params); 
            }           


            row.setTag(holder);
        } 
        else
        {
            holder = (MatrixHolder)row.getTag();
        }

        Option option = data[position];
        holder.txtTitle.setText(option.title);
        return row;

    }



    static class MatrixHolder {
        TextView txtTitle;
        RadioGroup group=null;
        int position;
        MatrixHolder(View row) {
            this.group=(RadioGroup)row.findViewById(R.id.radio_group1);
          }
    }
}

选项.java

package com.list.radiogroup;

public class Option {
     public String title;
      boolean isChecked   = false;

        public Option(){
            super();
        }

        public Option( String title) {
            super();
            this.title = title;
        }
    }
4

1 回答 1

2

列表视图中的复选框也有同样的问题。然后我发现它的发生是因为列表视图回收(Android有一个回收机制来节省内存)。你可以在谷歌搜索Android List view回收问题。

我用这个例子解决了它

http://www.mysamplecode.com/2012/07/android-listview-checkbox-example.html

我希望它会有所帮助

于 2013-09-19T06:56:30.157 回答