【问题标题】:Custom listview with checkbox. Custom adapter带有复选框的自定义列表视图。自定义适配器
【发布时间】:2015-03-01 10:38:45
【问题描述】:

简单的购物清单应用。 1个活动。 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(正好在它上面。不是listview 项目点击)使我的项目“isDone - true”并且textview 中的文本将改变颜色。 我的意思是,我想要 OnCheckedChangeListener 这会影响我的对象,改变它的 isDone 布尔值。

问题:

我可以在哪里以及如何输入OnCheckedChangeListener

【问题讨论】:

标签: android listview


【解决方案1】:

//在getView()中使用list维护checkBox状态

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-31
    • 2012-10-15
    • 2011-07-23
    相关资源
    最近更新 更多