简体   繁体   中英

AdapterView.OnItemClickListener() is not working in my customAdapter

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 .

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>

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM