【问题标题】:Click event from checkbox in custom listview in AndroidAndroid中自定义列表视图中复选框的单击事件
【发布时间】:2023-03-29 12:33:01
【问题描述】:

我的列表视图行有一个自定义布局:

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

我还有一个适配器来获取要显示的适当数据;它确实如此。从 UI 的角度来看,它看起来像我想要的。

但是,当我单击一个复选框时 - 没有任何反应。我想在后端存储我选择的项目列表(最好在活动类中)。

在我的活动类的 onCreate 中,我有这个代码:

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

我有 int 1 = 1;行在那里,这样我就可以添加一个断点来查看它是否被命中。它没有。我确定我做错了什么,比如它连接到列表视图行而不是复选框或其他东西 - 但我不确定如何将事件连接到复选框。

如果有人能指出我正确的方向,我将不胜感激。

谢谢

编辑:只是为了澄清

我在适配器中有这个:

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{

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

而且那个断点确实被命中了。 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.

【问题讨论】:

  • 你没有设置复选框的宽高文本属性

标签: android listview checkbox onclicklistener


【解决方案1】:

别这样!!!我几乎要疯了,试图让 ListView 中的小部件响应点击。不要将 Button、ImageButton 或 CheckBox 小部件放在 ListView 中。 TextViews 和 ImageViews 是要走的路。尝试对该 CheckBox 的单击做出反应以查找它所在的 ListView 项目,然后向 Activity 发送一些内容可能对您的健康非常有害。我试过了。

TextView + ImageView 可以在显示复选标记的图标和不显示复选标记的图标之间变化 - 模拟 CheckBox;

ImageView 本身可以模拟一个Button。

ImageView 需要设置为 focus=false。

首先,创建一个新类,其中包含要为 ListView 中的每个项目显示的字段。我制作了一个带有要显示的文本和一个布尔值来指示它是否被选中。为您的 ArrayList 和 ArrayAdapter 使用此类。

然后为 ListView 添加 setOnItemClickListener(),然后使用位置找到项目视图,然后获取新类的项目并切换其布尔值。

在 MyArrayAdapter.getView 方法中,getItem(position) 返回该项目的新类的实例。使用布尔值来确定 ImageView 使用什么图标。

当您需要知道 ListView 中哪些是“检查”和哪些未“检查”时,您只需遍历 ArrayList 并检查每个项目的布尔值。

【讨论】:

  • 从字面上看,在搞砸了这么长时间之后,我在诅咒自己为什么我什至会选择 crapLeesView -_-,我会选择 RV,因为我有同样的经历,好奇地选择了 LV 和现在 :D :p 那条线对健康有害 :D
【解决方案2】:

我想通了。

在适配器中我添加了这个: _activity 是从调用活动传递到构造函数的活动。根据 getView 中的位置和构建适配器时传入的数据,myObj 在代码的前面被声明为 final。

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

在活动中我添加了这个:

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

希望这对某人有所帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多