带复选框的自定义列表视图。定制适配器

Custom listview with checkbox. Custom adapter

简单的购物清单应用程序。 1 activity。 1 个定制适配器。 1 列表视图。 TextViewCheckBox:

的自定义行

列表视图:

<ListView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/shopListView"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_alignParentEnd="false"
    android:layout_alignParentBottom="false"
    android:layout_below="@+id/toolbar"/>

自定义行:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/shopListItem">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Test TEST"
        android:id="@+id/itemTextView"
        android:layout_gravity="start"
        android:textSize="25sp"
        android:paddingLeft="10dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp" />

    <LinearLayout android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignBottom="@id/itemTextView"
        android:layout_alignParentRight="true">
        <CheckBox
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/doneCheckBox"
            android:checked="false"
            android:focusable="false"
            android:focusableInTouchMode="false"
            android:layout_gravity="center"/>
    </LinearLayout>
</RelativeLayout>

我自己的适配器(扩展BaseAdapter):

public class ShopAdapter extends BaseAdapter {
    private Context mainContex;
    private ArrayList<ShopItem> shopItems;

    public ShopAdapter(Context mainContex, ArrayList<ShopItem> shopItems) {
        this.mainContex = mainContex;
        this.shopItems = shopItems;
    }

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

    @Override
    public Object getItem(int position) {
        return shopItems.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ShopItem shopItem = shopItems.get(position);

        View item = convertView;
        if (item == null) {
            item = LayoutInflater.from(mainContex).inflate(R.layout.shoplist_item, null);
        }

        TextView itemTextView = (TextView) item.findViewById(R.id.itemTextView);
        itemTextView.setText(shopItem.getDescription());
        CheckBox doneCheckBox = (CheckBox)item.findViewById(R.id.doneCheckBox);
        if (shopItem.isDone()){
            doneCheckBox.setChecked(true);
        }
        else {
            doneCheckBox.setChecked(false);
        }
        return item;
    }
}

项目:

public class ShopItem {

    private String description;
    private boolean done;

    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public boolean isDone() {
        return done;
    }

    public void setDone(boolean done) {
        this.done = done;
    }
}
如您所见,

项目 (ShopItem) 存储在 ArrayList "ShopItems" 中。

我想要的: 单击 CheckBox(完全正确。不是列表视图项目单击)使我的项目“isDone - true”,textview 中的文本将改变颜色。 我的意思是,我想要 OnCheckedChangeListener 这会影响我的对象,改变它的 isDone 布尔值。

问题:

在哪里以及如何放置 OnCheckedChangeListener

// 在 getView() 内部使用 list

维护复选框状态
doneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                doneCheckBox.ischecked=true;
            }
            else{
                doneCheckBox.ischecked=false;
            }
        }
    });