带复选框的Android listview

带复选框的Android listview,android,Android,在我的例子中,listview中有两个文本视图和一个复选框。我正在使用ArrayAdapter类设置TextView中的数据 当我选中任何复选框并在列表视图中滚动时,一些复选框会自动选中。我不明白为什么会发生这种情况。下面是我的代码。请查看代码并给出解决方案 public class Contacts_NumberActivity extends Activity { /** Called when the activity is first created. */ String

在我的例子中,listview中有两个文本视图和一个复选框。我正在使用ArrayAdapter类设置TextView中的数据

当我选中任何复选框并在列表视图中滚动时,一些复选框会自动选中。我不明白为什么会发生这种情况。下面是我的代码。请查看代码并给出解决方案

public class Contacts_NumberActivity extends Activity {
    /** Called when the activity is first created. */
    String nameList[] = { "U", "A", "B", "C", "D", "E", "F", "G", "H",
            "I", "J", "L", "M", "N", "O", "P", "Q" }, numberList[] = { "1",
            "2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4",
            "5", "6", "7" };
    ListView contactList;
    boolean[] checked;
    ContactHolder holder = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        contactList = (ListView) findViewById(R.id.contact_list);

        contactList.setAdapter(new ContactAdapter(this, R.layout.list_item,
                nameList, numberList));

    }

    public class ContactAdapter extends ArrayAdapter<String> {

        Context context;
        int layoutResourceId;
        String data[] = null;
        String data1[] = null;

        public ContactAdapter(Context context, int layoutResourceId,
                String[] data, String[] data1) {
            super(context, layoutResourceId, data);
            this.layoutResourceId = layoutResourceId;
            this.context = context;
            this.data = data;
            this.data1 = data1;
        }

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

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

                holder = new ContactHolder();
                holder.name = (TextView) row.findViewById(R.id.name);
                holder.number = (TextView) row.findViewById(R.id.number);
                holder.chkbox = (CheckBox) row.findViewById(R.id.checkBox1);

                holder.chkbox
                        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

                            @Override
                            public void onCheckedChanged(
                                    CompoundButton buttonView, boolean isChecked) {

                                holder.chkbox.setChecked(isChecked);
                                System.out.println("Checked possition= "
                                        + isChecked);
                            }

                        });

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

            holder.name.setText(data[position]);
            holder.number.setText(data1[position]);

            return row;
        }

        public class ContactHolder {
            TextView name, number;
            CheckBox chkbox;
        }
    }
}
public class Contacts\u NumberActivity扩展活动{
/**在首次创建活动时调用*/
字符串名称列表[]={“U”、“A”、“B”、“C”、“D”、“E”、“F”、“G”、“H”,
“I”、“J”、“L”、“M”、“N”、“O”、“P”、“Q”},数字列表[]={“1”,
"2", "3", "4", "5", "6", "7", "8", "9", "0", "1", "2", "3", "4",
"5", "6", "7" };
列表视图联系人列表;
布尔[]已选中;
ContactHolder=null;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contactList=(ListView)findViewById(R.id.contact\u list);
contactList.setAdapter(新ContactAdapter(此,R.layout.list_项,
姓名列表,数字列表);
}
公共类ContactAdapter扩展了ArrayAdapter{
语境;
国际布局资源;
字符串数据[]=null;
字符串data1[]=null;
公共ContactAdapter(上下文上下文,int-layoutResourceId,
字符串[]数据,字符串[]数据1){
超级(上下文、布局资源ID、数据);
this.layoutResourceId=layoutResourceId;
this.context=上下文;
这个数据=数据;
this.data1=data1;
}
@凌驾
公共视图getView(最终整型位置,视图转换视图,
视图组(父级){
视图行=转换视图;
if(行==null){
充气机=((活动)上下文)
.getLayoutInflater();
行=充气机。充气(layoutResourceId,父级,false);
保持架=新触点保持架();
holder.name=(TextView)row.findViewById(R.id.name);
holder.number=(TextView)row.findViewById(R.id.number);
holder.chkbox=(复选框)row.findViewById(R.id.checkBox1);
holder.chkbox
.setOnCheckedChangeListener(新的OnCheckedChangeListener(){
@凌驾
检查更改后的公共无效(
复合按钮视图,布尔值已选中){
holder.chkbox.setChecked(已检查);
System.out.println(“已检查的位置=”
+检查);
}
});
row.setTag(支架);
}否则{
holder=(ContactHolder)行。getTag();
}
holder.name.setText(数据[位置]);
holder.number.setText(数据1[位置]);
返回行;
}
公共类联系人{
文本视图名称、编号;
复选框chkbox;
}
}
}

您的代码没有问题,但问题在于ArrayAdapter的getView函数 调用的方式是重新初始化和重用列表中未聚焦的行,您正在使用convert view check,这会导致分页,就好像在屏幕中选中first复选框,然后分页中的所有屏幕都选中first复选框一样

要解决这个问题,您应该使用布局,而不是使用适配器的列表视图。这里有一个提示可以帮助您

LinearLayout parent = new LinearLayout(this);
parent.setOrientation(VERTICAL);
parent.setLayoutParams(new LayoutParams(list_width,list_height);

for(number of rows in your list)
{
LinearLayout row= new LinearLayout(this);
row.setOrientation(Horizontal);
row.setLayoutParams(new LayoutParams(row_width,row_height);

TextView text= new TextView (this);
text.setText("your text");
text.setLayoutParams(new LayoutParams(text_width,text_height);
row.addView(text);
.
.//other view you want to add as check box etc
.
.
parent.AddView(row);
}

this.addView(parent);

hope this will help you

注意:如果您有大量行,可能会导致内存问题

请参阅此链接。这可能会很有帮助。。我想打印您的列表视图可能有问题。请检查您的列表视图。它可能有重复的数据,这就是为什么您感觉像是自动选中了复选框。检查并让我知道,当我单击任何复选框并滚动列表时,会发生这种情况。下面的复选框也被选中。检查下面的链接可能会对您有所帮助