簡體   English   中英

AdapterView.OnItemClickListener()在我的customAdapter中不起作用

[英]AdapterView.OnItemClickListener() is not working in my customAdapter

請參考這張圖片https://www.dropbox.com/s/6zoj9lw10oc07xa/to_dropbox.png

what i am doing :我正在創建一個list_view,在其中添加自定義適配器。

what i am using :我正在使用listView,customAdapter,menuitem。 listView :在整個應用程序單列表視圖customadapters :3個的定制適配器menuitem :1

How i am implementing :我有一個數據庫,可以從該數據庫中正確提取內容,並且通過過濾3種類型的數據在列表視圖中輸入了這些值:默認情況下(在onCreate中)輸入第一個adapter_type。

adapter = new Adapter_forCompletedReminder( array_today_title , this) ;
ls.setAdapter(adapter) ;

按下menuitem在我的列表視圖中輸入第二個adapter_type。

adapter = new Adapter_forCompletedReminder( array_past_2_day_title , this) ;
ls.setAdapter(adapter) ;

按下menuitem在我的列表視圖中輸入第三個adapter_type。

adapter = new Adapter_forCompletedReminder( array_other_day_title , this) ;
ls.setAdapter(adapter) ;

what is my problem :此代碼添加在onCreate()方法內部。

ls.setOnItemClickListener( new AdapterView.OnItemClickListener() 
{
    public void onItemClick(AdapterView<?> adapterView , View view , int position ,long arg3) 
    {
        Log.i("Item clicked","tushar:itemclicked") ;
    }
});

當我嘗試實現AdapterView.OnItemClickListener()時,它無法正常工作……代碼沒有崩潰(log cat中沒有紅線)。 單擊llist_view_element時代碼未執行

謝謝你閱讀我的問題。

您使用customview_completedxml_listview.xml中的復選框,這就是為什么onItemClick偵聽器不起作用的原因。 如果您在復選框中設置clickable =“ false”,則onItemclick偵聽器將起作用。

如果您希望該復選框仍然有效,則必須在自定義適配器類中設置onclicklistener事件。

//我編輯getView

 @Override
  public View getView(int position, View convertView, ViewGroup parent)  
   { 
    LayoutInflater inflater = LayoutInflater.from(ob) ; 
    View v = inflater.inflate(R.layout.customview_completedxml_listview, null ) ; 


     TextView txt = ( TextView ) v.findViewById(R.id.txt_fordisplayingdata) ; 
      txt.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
             Toast.makeText(ob, "Hello", Toast.LENGTH_SHORT).show();

        }
    });
      txt.setText(recieved_Array[position]) ; 

      return v ; 
   } 

////////////////////// //第二個解決方案在復選框中設置了android:focusable =“ false”

     <?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="50dp"
       android:orientation="horizontal" 
       > 

    <TextView 
    android:id="@+id/txt_fordisplayingdata"
    android:layout_width="240dp"
    android:text="display data"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    /> 

  <TextView 
    android:id="@+id/txt_fordisplayingLargerdata"
    android:layout_width="240dp"
    android:text="display data larger bahut larger "
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    android:layout_marginLeft="10dp"
    android:visibility="gone"
    /> 

  <View
    android:layout_width="2dp"
    android:layout_toRightOf="@id/txt_fordisplayingdata"
    android:layout_height="15dp"
    android:layout_marginLeft="15dp"
    android:layout_centerVertical="true"
    android:id="@+id/view_forcompletedtask"
    /> 


  <CheckBox 
    android:layout_toRightOf="@id/view_forcompletedtask"
    android:id="@+id/checkbox_tocomplete"
    android:layout_marginLeft="15dp"
    android:layout_width="wrap_content"
    android:focusable="false"
    android:layout_height="wrap_content"
    android:layout_centerVertical="true"
    /> 

</RelativeLayout>

您可以嘗試以下幾種方法:-

  1. 如果您的listview項中有任何按鈕(或復選框)或任何元素可以處理click事件,則對每個元素執行以下操作:-

     android:focusable = "false" android:focusableInTouchMode = "false" 
  2. 嘗試設定這個

     list.setItemsCanFocus(false); 
  3. 重寫onItemClick()方法

     ls.setOnItemClickListener( new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView , View view , int position ,long arg3) { Log.i("Item clicked","tushar:itemclicked") ; } }); 

我真的不能說您到底有什么問題,但是我為您寫了一個非常簡單的示例。 嘗試一下,如果可行,只需將當前項目移植到我的示例項目中即可。 https://docs.google.com/file/d/0Bz4Xd7Ju_kbYbVlyd1dvYTJZYTg/edit?usp=sharingalways

PS:當您完成構想(關於ViewHolder模式)時,建議您閱讀有關“ Android最佳實踐”的信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM