AdapterView.OnItemClickListener() is not working in my customAdapter

蹲街弑〆低调 提交于 2019-12-05 09:16:21

You use checkbox in customview_completedxml_listview.xml that is why onItemClick listener is not working. If you set clickable = "false" in checkbox then onItemclick listener will work.

If you want want that checkbox will stil work then you have to set onclicklistener event in you custom adapter class.

// I edit 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 ; 
   } 

/////////////////////// // Second solution set android:focusable="false" in checkbox

     <?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>
amrinder007

Here are few things you can try :-

  1. If there is any button(or checkbox) or any element in your listview item which handles click event then do this for each element:-

    android:focusable = "false"  
    android:focusableInTouchMode = "false"
    
  2. Try setting this

    list.setItemsCanFocus(false);
    
  3. Override the onItemClick() method

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

I really can't say what exactly problem you have, but I wrote very simple example for you. Try it, and if it works - just port your current project into my sample project. https://docs.google.com/file/d/0Bz4Xd7Ju_kbYbVlyd1dvYTJZYTg/edit?usp=sharingalways

P.S.: I recommend you to read about "best practices in Android", when you finish your idea ( about ViewHolder pattern).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!