Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 带有复选框的自定义listview。自定义适配器_Android_Listview - Fatal编程技术网

Android 带有复选框的自定义listview。自定义适配器

Android 带有复选框的自定义listview。自定义适配器,android,listview,Android,Listview,简单的商店名单应用程序。1活动。1个自定义适配器。1 listview。带有文本视图和复选框的自定义行: 列表视图: <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/shopListView" android:layout_alignParentLeft="true" android:layout_a

简单的商店名单应用程序。1活动。1个自定义适配器。1 listview。带有
文本视图
复选框的自定义行

列表视图:

<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>
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;
    }
}
项目:

<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>
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;
    }
}
如您所见,
ArrayList
“ShopItems”中存储的项目(
ShopItem

我想要什么: 单击
复选框
(正好在它上面。不要单击listview项目)使我的项目“
isDone
-true”,textview中的文本将更改颜色。 我的意思是,我想要
OnCheckedChangeListener
,它会影响我的对象,改变它的
isDone
布尔值

问题:

在checkedChangeListener上,我可以在哪里以及如何放置

//在getView()中,使用列表维护复选框状态

doneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                doneCheckBox.ischecked=true;
            }
            else{
                doneCheckBox.ischecked=false;
            }
        }
    });

在CustomAdapter中的getview()方法中在getview()中写入OnCheckedChangeListner()查看我的答案你会解决你的问题