2016-01-19 70 views
2

我的ListView沒有響應click事件。我附上佈局和java文件,請幫我解決這個問題。 在此先感謝。ListView不響應onClickListener

版式文件

<?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(); 
    } 
+0

大概在ListView項目是像的EditText等一個或多個可點擊元素元素c。發佈ListView項目佈局[xml文件]。 –

+0

是的,我在Listview項目佈局中有一些可點擊的元素...我刪除了這些東西,它開始working.Thanks節食者:) –

+0

嘗試設置此元素clickable =「false」,focusable =「false」 –

回答

0

我有這個問題,只是今天早上 !

你有內你的ListView的項目有些點擊查看,對我來說,我有一個單選按鈕

嘗試添加到您可點擊查看,也將努力:

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

你會發現這篇文章有幫助: OnItemCLickListener not working in listview

好運