CheckBox里面的ListView里面的ListFragment

时间:2012-12-03 15:56:12

标签: android listview checkbox

当我点击ListView中的复选框时,列表中的第二个项目也会被选中。例如,如果我检查第1项,则会检查列表中的第7项。这是我的单元格布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/relativeLayout1" android:layout_width="fill_parent"    android:layout_height="fill_parent"    xmlns:android="http://schemas.android.com/apk/res/android">

    <ImageView 
        android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:id="@+id/imgDisclosureIndicator" 
        android:src="@drawable/disclosureon" 
        android:layout_alignParentTop="true" 
        android:layout_alignParentRight="true" 
        android:paddingRight="6dp"></ImageView>

    <ImageView
        android:id="@+id/imgTaskState"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/taskok" />

    <CheckBox
        android:id="@+id/chkTaskImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/imgTaskState"
        android:layout_toLeftOf="@+id/imgDisclosureIndicator"
        android:layout_toRightOf="@+id/imgTaskState"
        android:focusable="false" />

</RelativeLayout>

我加入了一张图片来帮助你:

Checkbox problem

作为旁注,ListView位于ListFragment内。

编辑:下面是我的单元格及其数据的代码。

String[] from = {"taskId", "taskStateId", "taskText", "disclosureIndicatorId"};
                    int[] to = {0, R.id.imgTaskState, R.id.chkTaskImage, R.id.imgDisclosureIndicator};
SimpleAdapter adapter = new SimpleAdapter(mContext, mList, R.layout.task_cell, from, to);
setListAdapter(adapter);

编辑#2:如果ListView垂直显示,则不会发生这种情况。

ListView problem

编辑#3:即使我使用SimpleAdapter,使用以下代码,当ListView处于水平状态时,我也会遇到同样的错误。

private class CellInspectionAdapter extends BaseAdapter
    {
            private Vector<InspectionCell> cells;
            private Context context;

            public CellInspectionAdapter(Context context, Vector<InspectionCell> cells)
            {
                this.cells = cells;
                this.context = context;
        }

        @Override
        public int getCount()
        {
            return this.cells.size();
        }

        @Override
        public Object getItem(int position)
        {
            return null;
        }

        @Override
        public long getItemId(int position)
        {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            // Map data from the ArrayList from the adapter
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View celluleTask;

            if (convertView == null) 
            {
                celluleTask = new View(context);
                celluleTask = inflater.inflate(R.layout.laprise_task_cell, null);

                // Assign data to the cell
                ImageView imgEtat = (ImageView)celluleTask.findViewById(R.id.imgTaskState);
                imgEtat.setImageResource(cells.get(position).getStateDrawable());

                // Assign text
                CheckBox chkCell = (CheckBox)celluleTask.findViewById(R.id.chkTaskImage);
                chkCell.setText(cells.get(position).getTexte());

                // Disclosure
                ImageView imgDisclosure = (ImageView)celluleTask.findViewById(R.id.imgDisclosureIndicator);
                imgDisclosure.setImageResource(cells.get(position).getDisclosureDrawable());    
            }
            else
            {
                celluleTask = (View)convertView;
            }

            return celluleTask;
        }
    }
编辑#5:这是解决方案!覆盖getViewTypeCount()和getItemViewType(int position)方法。我已将这些行添加到SimpleAdapter中,它可以正常工作。

@Override
public int getViewTypeCount() {                 

    return getCount();
}

@Override
public int getItemViewType(int position) {

    return position;
}

1 个答案:

答案 0 :(得分:2)

您错误地使用了convertView。非空convertView只会让您无法创建新视图并执行布局工作,但您convertView的所有子项的内容设置为匹配当前项目。这也解释了为什么它在垂直方向工作,因为这两行都是同时可见的。

试试这个:

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // Map data from the ArrayList from the adapter
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        View celluleTask = convertView;

        if (celluleTask == null) {
            celluleTask = inflater.inflate(R.layout.laprise_task_cell, null);
        }

        // Assign data to the cell ALWAYS!
        ImageView imgEtat = (ImageView)celluleTask.findViewById(R.id.imgTaskState);
        imgEtat.setImageResource(cells.get(position).getStateDrawable());

        // Assign text
        CheckBox chkCell = (CheckBox)celluleTask.findViewById(R.id.chkTaskImage);
        chkCell.setText(cells.get(position).getTexte());

        // Disclosure
        ImageView imgDisclosure = (ImageView)celluleTask.findViewById(R.id.imgDisclosureIndicator);
        imgDisclosure.setImageResource(cells.get(position).getDisclosureDrawable());    

        return celluleTask;
}

P.S。:您还应该从getView方法中移除布局填充的提取,这样就不能一遍又一遍地完成。

P.P.S。:为避免一次又一次地呼叫findViewById,请查看 View Holder Pattern