AdapterView.OnItemClickListener() is not working in my customAdapter

18,358

Solution 1

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>

Solution 2

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") ;  
    }  
    });
    

Solution 3

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).

Share:
18,358
Tushar Pandey
Author by

Tushar Pandey

Capitaine Train animation https://onedrive.live.com/?cid=d6874f5e96bbf5a8&amp;id=D6874F5E96BBF5A8%2117014&amp;ithint=folder,&amp;authkey=!ANgRn7phAAB0Cgk Floating Edit Text &amp; Ripple effect on pre-lollipop devices https://onedrive.live.com/redir?resid=D6874F5E96BBF5A8!16412&amp;authkey=!ACvnG5_O9z4Q-Q0&amp;ithint=folder%2cclasspath for flip-rotation like flipkart addtocart symbol in android view http://stackoverflow.com/a/27354046/2382964 Country-CountryCodes-Country Flags http://hutofandroid.blogspot.in/2014/09/country-country-codes-country-flags-api.html Remotely Android crash report generation http://stackoverflow.com/a/26988154/2382964

Updated on June 13, 2022

Comments

  • Tushar Pandey
    Tushar Pandey almost 2 years

    please refer this image https://www.dropbox.com/s/6zoj9lw10oc07xa/to_dropbox.png

    what i am doing : i am creating a list_view , in which i am adding custom adapter .

    what i am using : i am using , listView , customAdapter , menuitem . listView : single listview in whole application customadapters : 3 custom adapters menuitem : 1

    How i am implementing : i have data base from which things are fetched properly , and from that database i have entered these values in my listview by filtering that data in 3 types : 1st adapter_type is entered by default ( in onCreate ) .

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

    2nd adapter_type is entered in my listview by pressing menuitem .

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

    3rd adapter_type is entered in my listview by pressing menuitem .

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

    what is my problem : this code is added inside onCreate() method .

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

    when i have tried to implement AdapterView.OnItemClickListener() , it is not working ... code is not crashing ( no red lines in log cat ). code is not executing in the click of llist_view_element

    thanks , for reading my problem .

  • Tushar Pandey
    Tushar Pandey almost 11 years
    I will , but it is a long .. project there are 6-7 sections inside it .
  • Tushar Pandey
    Tushar Pandey almost 11 years
    try to add onClickListener to your view in getView() method (before return v :) this Answer is in the way to Rock .
  • Veaceslav Gaidarji
    Veaceslav Gaidarji almost 11 years
    just open project and test if works. after that you can compare your project with this example and possible you will find bug "why onClick doesn't work". onClickListener for view in getView() not needed in your project, because you have simple list logic. seems like you have redundant attributes in your xml layouts.
  • Veaceslav Gaidarji
    Veaceslav Gaidarji almost 11 years
    you using checkBox in your layout, that was intercepting touch events on your list row. set next property for your checkBox: "clickable = false" in customview_completedxml_listview.xml
  • Pawan Yadav
    Pawan Yadav almost 11 years
    Hello Tushar Pandey I hope it will work i provide two solution both will work select according to your implementation
  • Sti
    Sti over 10 years
    Thanks, nr.1 worked for me. Answers on other posts suggested android:clickable="false" would do the trick alone, but not for me. The 'focusable's worked perfect. Maybe there's a difference between emulator and device here..
  • Andrew Dunai
    Andrew Dunai almost 9 years
    You saved me. Thanks!