带有复选框的Android列表视图

时间:2012-02-28 06:49:53

标签: android

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

当我选中任何复选框并在列表视图中滚动时,会自动选中一些复选框。我不知道为什么会发生这种情况,请查看代码并提供解决方案以解决此问题。

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

1 个答案:

答案 0 :(得分:0)

您的代码没有问题,但问题在于ArrayAdapter的getView功能 正在调用重新初始化和重用未重点列表的方式,因为您正在使用转换视图检查,这会导致分页,就好像在屏幕中您先检查复选框,然后分页中的所有屏幕都会选中第一个复选框,

要从此解决你应该使用布局而不是列表视图使用适配器这里是一个提示可能会帮助你

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

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

相关问题