简体   繁体   中英

Click event from checkbox in custom listview in Android

I have a custom layout for my list view rows:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/listSelector"
    android:orientation="horizontal">

    <LinearLayout
            android:id="@+id/checkboxSelection1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:padding="3dip">

        <CheckBox android:id="@+id/checkbox1" />

        </LinearLayout>

    <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/checkbox1"
            android:orientation="vertical">

            <TextView
                android:id="@+id/text1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/text2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />
    </LinearLayout>
</RelativeLayout>

I've also got an adapter to get the appropriate data to display; which it does. From the UI perspective, it looks like I want it to.

However, when I click a checkbox - nothing happens. I want to store a list of the items I've selected in the backend (in the activity class ideally).

In my onCreate in the activity class, I've got this code:

listView.setAdapter(adapter);

listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);

// Click event for single list row
listView.setOnItemClickListener(new OnItemClickListener() 
{
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) 
    {
        int i = 1;
    }
});

I've got the int 1 = 1; line there just so I could add a breakpoint to see if it gets hit. It doesn't. I'm sure I'm doing something wrong, like it's hooked up to the list view row instead of the checkbox or something - but I'm not sure how I can hook the event up to the checkbox.

If anyone could point me in the right direction I would appreciate it.

Thanks

Edit: JUST TO CLARIFY

I have this in the adapter:

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{

    public void onCheckedChanged(CompoundButton arg0, boolean arg1) 
    {
        // TODO Auto-generated method stub
        int i = 1;
    }
});

And that breakpoint does get hit. So I'm just trying to find out how I get have an event get raised in the activity, instead of just the adapter, when a checkbox is selected or unselected.

Don't do it!!! I almost went mad trying to get widgets in a ListView to respond to clicks. Do not put Button, ImageButton, or CheckBox widgets in a ListView. TextViews and ImageViews are the way to go. Trying to react to that click on that CheckBox to find what ListView item it is in and then send something to the Activity could be very harmful to your health. I tried.

TextView + ImageView that can vary between an icon showing a checkmark and one without - simulate a CheckBox;

ImageView by itself can simulate a Button.

The ImageView needs to be set to focus=false.

First, create a new class that has the fields you want to display for each item in the ListView. I made one with the text to display and a boolean that indicates whether it is checked or not. Use this class for your ArrayList and ArrayAdapter.

Then you add the setOnItemClickListener() for the ListView, then use the position to find the item view, and then get the item of your new class and toggle its boolean.

In the MyArrayAdapter.getView method, getItem(position) returns the instance of the new class for that item. Use the boolean to determine what icon to use for the ImageView.

When you need to know what is and isn't "checked" in the ListView, you just go through the ArrayList and check the boolean for each item.

I figured it out.

In the adapter I added this: With _activity being the activity passed into the constructor from the calling activity. myObj is declared as a final earlier up in the code, based on the position in getView and the data passed in when the adapter was constructed.

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
        {
            @Override
            public void onCheckedChanged(CompoundButton button, boolean checked) 
            {
                // Cast it so we can access the public functions
                MyActivity myActivity = (MyActivity) _activity;

                if (checked) // true if the checkbox is checked, false if unchecked
                {
                    myActivity.checkboxSelected(myObj);
                }
            }
        });

And in the activity I added this:

public void checkboxSelected(MyObj myObj)
{
    // Do stuff with myObj here
}

Hopefully this helps someone.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM