android - Android 中自定义 ListView 中复选框的单击事件

标签 android listview checkbox onclicklistener

我的 ListView 行有一个自定义布局:

<?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 的角度来看,它看起来像我想要的。

但是,当我单击一个复选框时,没有任何反应。我想在后端存储我选择的项目列表(理想情况下在 Activity 类中)。

在 Activity 类的 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;在那里行,这样我就可以添加一个断点来查看它是否被命中。它没有。我确定我做错了什么,比如它连接到 ListView 行而不是复选框或其他东西 - 但我不确定如何将事件连接到复选框。

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

谢谢

编辑:只是为了澄清

我在适配器中有这个:

taskChecked.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{

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

而且那个断点确实被击中了。因此,我只是想了解如何在选中或取消选中复选框时在 Activity 中引发事件,而不仅仅是适配器。

最佳答案

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

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

ImageView 本身可以模拟一个 Button。

ImageView需要设置为focus=false。

首先,创建一个新类,其中包含要为 ListView 中的每个项目显示的字段。我用要显示的文本和一个指示是否选中的 bool 值制作了一个。将此类用于您的 ArrayList 和 ArrayAdapter。

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

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

当您需要知道 ListView 中什么是“选中”和什么没有“选中”时,您只需浏览 ArrayList 并检查每个项目的 bool 值。

关于android - Android 中自定义 ListView 中复选框的单击事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19720284/

相关文章:

android - 在 threadpoolexecutor 中停止运行任务的最佳方法是什么?

android - Gradle:构建Android库后如何运行自定义任务?

html - 对齐分层复选框

javascript - Chrome 扩展程序中的复选框未触发更改事件

java - Android Studio:PC关闭后库的内存不足(Java堆空间)错误

Android:如何创建自定义按钮小部件

listview - 如何将点击传递给顶部小部件下方的小部件?

php - listView中按日期显示数据

c# - Xamarin.Forms 分组 ListView 无法在 Android 中添加新组

javascript - 获取选中复选框的值?