【问题标题】:ListView doesn't respond to onClickListenerListView 不响应 onClickListener
【发布时间】:2016-01-19 18:04:27
【问题描述】:

我的 Listview 没有响应点击事件。我正在附加布局和 java 文件,请帮助我解决这个问题。 提前致谢。

布局文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_green"
    android:descendantFocusability="beforeDescendants"
    android:focusableInTouchMode="true"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin" >

    <TextView
        android:id="@+id/txt_mseTitle"
        style="@style/pageTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="5dp"
        android:text="@string/report" />

    <View
        android:id="@+id/txt_mseView"
        android:layout_width="match_parent"
        android:layout_height="2dip"
        android:layout_below="@+id/txt_mseTitle"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#2E9AFE" />

    <EditText
        android:id="@+id/edt_OrderLst_Search"
        style="@style/EditTextAppTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/txt_mseView"
        android:layout_marginLeft="3dp"
        android:layout_marginTop="22dp"
        android:gravity="center"
        android:hint="@string/search" />

    <HorizontalScrollView
        android:id="@+id/scroll_full"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@+id/edt_OrderLst_Search" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <LinearLayout
                android:id="@+id/lay_fullTitle"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="16dp"
                android:background="#000000"
                android:orientation="horizontal"
                android:padding="5dp" >

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="200dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".3"
                    android:text="@string/mpr_po_no"
                    android:textStyle="bold" >
                </TextView>

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="300dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".3"
                    android:text="@string/mprrcptno"
                    android:textStyle="bold" >
                </TextView>

                <TextView
                    style="@style/textRegular_list"
                    android:layout_width="130dp"
                    android:layout_height="wrap_content"
                    android:layout_weight=".3"
                    android:text="@string/mpr_vendor_no"
                    android:textStyle="bold" >
                </TextView>
            </LinearLayout>

            <ListView
                android:id="@+id/lstSelOrder"
                style="@style/ListViewAppTheme.White"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </ListView>
        </LinearLayout>
    </HorizontalScrollView>

</RelativeLayout>

.java 文件

这是我的类文件,其中 onItemClickListener 和长按也不响应点击事件

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mprreport);

        registerBaseActivityReceiver();

        LayoutInflater inflater = getLayoutInflater();
        toastLayout = inflater.inflate(R.layout.toast,
                (ViewGroup) findViewById(R.id.toast_layout_root));
        toastText = (TextView) toastLayout.findViewById(R.id.text);
        lstSel = (ListView) findViewById(R.id.lstSelOrder);
        edtSearch = (EditText) findViewById(R.id.edt_OrderLst_Search);

        dbhandler = new DatabaseHandler(this);

        // Inflating List
        dbhandler.getReadableDatabase();
        List<MPR_Trans> lstTemp = dbhandler.getAllPoTrans();
        dbhandler.closeDatabase();

        transAdap = new TransactionAdapter(MprReport.this, lstTemp);
        lstSel.setAdapter(transAdap);

        lstSel.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Intent i = new Intent(MprReport.this, MprForPrint.class);
                startActivity(i);

            }
        });

        edtSearch.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence cs, int arg1, int arg2,
                    int arg3) {
                MprReport.this.transAdap.getFilter().filter(cs);
            }

            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub

            }

            @Override
            public void afterTextChanged(Editable edtxt) {
                // TODO Auto-generated method stub
            }
        });

        lstSel.setOnItemLongClickListener(new OnItemLongClickListener() {
            // setting onItemLongClickListener and passing the
            // position to the function
            @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
                    final int position, long arg3) {
                final AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                        MprReport.this);
                alertDialog.setTitle("Confirmation");
                alertDialog.setIcon(R.drawable.warning);
                alertDialog.setCancelable(false);
                alertDialog.setPositiveButton("Yes",
                        new DialogInterface.OnClickListener() {

                            public void onClick(DialogInterface dialog,
                                    int which) {

                                MPR_Trans mpr_Trans = new MPR_Trans();

                                mpr_Trans = (MPR_Trans) lstSel
                                        .getItemAtPosition(position);
                                String poNo = mpr_Trans.getPoNumber();
                                String rcptNo = mpr_Trans.getReceiptNumber();

                                // Deleting from Temp table
                                dbhandler.getWritableDatabase();
                                dbhandler.deleteFromMprTransTable(poNo, rcptNo);
                                dbhandler.closeDatabase();

                                dbhandler.getReadableDatabase();
                                List<MPR_Trans> lstTemp = dbhandler
                                        .getAllPoTrans();
                                dbhandler.closeDatabase();

                                transAdap = new TransactionAdapter(
                                        getApplicationContext(), lstTemp);
                                lstSel.setAdapter(transAdap);

                                transAdap.notifyDataSetChanged();
                            }
                        });
                alertDialog.setNegativeButton("Cancel",
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int which) {

                            }
                        });
                alertDialog.setMessage("Do you want Delete this Item ?");

                alertDialog.show();

                return true;
            }
        });

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        unRegisterBaseActivityReceiver();
    }

【问题讨论】:

  • 可能在 ListView 项目中是一个或多个可点击元素元素,如 EditText 等。发布 ListView 项目布局 [xml 文件]。
  • 是的,我的 Listview 项目布局中有一些可点击的元素...我删除了这些东西,它开始工作。谢谢节食者:)
  • 尝试在这个元素上设置 clickable="false", focusable="false"

标签: java android android-layout listview


【解决方案1】:

今天早上我遇到了这个问题!

您的列表视图项目中有一些可点击的视图,对我来说,我有一个单选按钮

尝试将此添加到您的可点击视图中,它会起作用:

   android:clickable="false"
   android:focusable="false"
   android:focusableInTouchMode="false"

您可能会发现这篇文章很有帮助: OnItemCLickListener not working in listview

祝你好运

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多