0

Simple shop list app. 1 activity. 1 custom adapter. 1 listview. Custom rows with TextView and CheckBox:

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"/>

Custom row:

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

My own adapter (extends 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;
    }
}

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

Items (ShopItem) stored in ArrayList "ShopItems" as you can see.

What I want: Click on CheckBox (exactly on it. not listview item click) makes my item "isDone - true" and text in textview will change color. I mean, I want OnCheckedChangeListener which will affect my object, changing its isDone boolean.

QUESTION:

Where and how can I put OnCheckedChangeListener?

3
  • inside your getview() Method inside customadapter Commented Jan 3, 2015 at 9:19
  • write the OnCheckedChangeListner() in getView() Commented Jan 3, 2015 at 9:19
  • stackoverflow.com/questions/26190444/… look at my answer u will solve your issue Commented Jan 3, 2015 at 9:22

1 Answer 1

1

// inside the getView() maintain the checkBox state using list

doneCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
             if(isChecked){
                doneCheckBox.ischecked=true;
            }
            else{
                doneCheckBox.ischecked=false;
            }
        }
    });
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.