【问题标题】:why do not work onclicklistener on listView?为什么不在listView 上的onclicklistener 上工作?
【发布时间】:2015-09-28 13:29:51
【问题描述】:

我使用 textview 和 checkbox 作为 listView 的项目。我为 listview 编写的代码工作正常。但是,当我在 listview 中添加复选框时,onclicklistener 事件现在不起作用。我在 xml 文件中使用此代码:

我的xml代码:

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:weightSum="5" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <TextView
        android:id="@+id/textView_Item_Listview"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="4"
        android:gravity="right"
        android:padding="3dp"
        android:textSize="25sp" />

</LinearLayout>

我的java代码:

private class Adapter_collection extends ArrayAdapter<String> {

    public Adapter_collection(Context context, int resource, int textViewResourceId,
            String[] name_collection_tbl_collection) {
        super(context, resource, textViewResourceId, name_collection_tbl_collection);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = inflater.inflate(R.layout.listview_items, parent, false);
        TextView txt_item_list_collection = (TextView) row.findViewById(R.id.textView_Item_Listview);
        CheckBox checkBox=(CheckBox)row.findViewById(R.id.checkBox_Item_Listview);

        checkBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked ) {
            Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
            }
        });
        txt_item_list_collection.setText(name_collection_tbl_collection[position]);
        return row;
    }

}


list_collections.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
            Intent goTo_SubjectsActivity = new Intent(Collection_List_Activity.this, Subjects_Activity.class);
            startActivity(goTo_SubjectsActivity);
            Intent_values.id_collection = id_tbl_collection[arg2];
        }
    });

【问题讨论】:

  • 也期待一些 java 代码?
  • 你的java代码在哪里?
  • 也许您需要使用 onItemClickListener 而不是 onClickListener?
  • TextViewCheckBox设置android:focusable="false"android:focusableInTouchMode="false"

标签: android listview android-listview


【解决方案1】:

可能的原因是如果您的List_item 中有touchable elements,它将阻止list_item 触摸,因此您的onItemclickListener 上的onItemclickListener 无法工作。 如果你想在你的 ListView 中实现 onClickListener,你可以添加

android:descendantFocusability="blocksDescendants"

list_itemLinearLayout 内部的父级

【讨论】:

  • Rajan KS:我为 TextView 和 CheckBox 设置了 android:focusable="false" 和 android:focusableInTouchMode="false"。它解决了。谢谢你的回答
【解决方案2】:

尝试将 onclicklistener 分配给 textview,而不是列表。

    private class Adapter_collection extends ArrayAdapter<String> {

...

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View row = inflater.inflate(R.layout.listview_items, parent, false);
            TextView txt_item_list_collection = (TextView) row.findViewById(R.id.textView_Item_Listview);

            //add this
            txt_item_list_collection.setOnClickListener(new OnClickListener() { ... });

            txt_item_list_collection.setText(name_collection_tbl_collection[position]);
            return row;
        }

    }

【讨论】:

  • 感谢您的回答。我想为复选框实现 setOnCheckedChangeListener () 并在此方法中获取复选框的位置。我像你的回答一样实施但是位置范围出现错误。你能指导我吗
  • 您可以像使用findViewById 对textview 一样获得对CheckBox 的引用,然后只需为其添加一个监听器。
  • 那是代码,不是错误信息?顺便提一句。请在您的问题中发布代码作为编辑,我无法在这里阅读!
  • 我更改了 Adapter_collection 类。此行中存在错误:Toast.makeText(getApplicationContext(), String.valueOf(position), Toast.LENGTH_SHORT).show();
猜你喜欢
  • 1970-01-01
  • 2023-02-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-15
  • 2013-05-06
  • 2011-10-02
相关资源
最近更新 更多