ListView不响应onClickListener

时间:2016-01-19 18:04:27

标签: java android android-layout listview

我的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();
    }

1 个答案:

答案 0 :(得分:0)

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

你在listview的项目中有一些可点击的视图,对我来说,我有一个radioButton

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

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

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

祝你好运