Delete button in listview Item appears in another position when longclick

I’d like to put a delete Button on each on my listview items. This button has to appear when the user longClicks on the textview Inside thelistView Item :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="@drawable/mybackground"
    android:orientation="horizontal" >


 <EditText
     android:id="@+id/edittext"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="2dip"
     android:layout_weight="2"
     android:inputType="textMultiLine"
     android:singleLine="false"
     android:textSize="12dp"
     android:visibility="gone" >

 </EditText>

 <TextView
     android:id="@+id/textview"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_marginLeft="2dip"
     android:layout_weight="2"
     android:singleLine="false"
     android:textColor="#000000"
     android:textSize="14dp" >

 </TextView>

 <ImageButton
     android:id="@+id/editButton"
     android:layout_width="40dp"
     android:layout_height="40dp"
     android:src="@drawable/ic_menu_close_clear_cancel"
     android:visibility="gone" />

 </LinearLayout>

Here is the java code :

      public View getView(int position, View convertView, ViewGroup parent) {

           ViewHolder holder = null;

           if (convertView == null) {
               LayoutInflater vi = (LayoutInflater)getSystemService(
               Context.LAYOUT_INFLATER_SERVICE);
               convertView = vi.inflate(R.layout.affichageitem, null);                
               holder = new ViewHolder();
               holder.text = (TextView) convertView.findViewById(R.id.textview);
               holder.button = (ImageButton) convertView.findViewById(R.id.editButton);
               convertView.setTag(holder);

               holder.button.setOnClickListener( new View.OnClickListener() {                   
                     public void onClick(View v) { 
                         ListView listView0=(ListView)findViewById(R.id.listView0);              
                         Toast.makeText(getApplicationContext(),"Clicked on position : " + listView0.getPositionForView(v),Toast.LENGTH_SHORT).show();
                     } 
               }); 

               holder.text.setOnClickListener( new View.OnClickListener() { 
                     public void onClick(View v) { 
                          TextView tv = (TextView) v ; 
                          Toast.makeText(getApplicationContext(),"Clicked on app : " + tv.getText(),Toast.LENGTH_LONG).show();
                         } 
                }); 


               holder.text.setOnLongClickListener( new View.OnLongClickListener() { 
                     public boolean onLongClick(View v) { 
                          TextView tv = (TextView) v ;
                          ImageButton editButton = (ImageButton) findViewById(R.id.editButton);
                          editButton.setVisibility(View.VISIBLE);
                          editButton.setOnClickListener( new View.OnClickListener() { 
                                 public void onClick(View v) { 
                                      ImageButton iB = (ImageButton) v ; 
                                      ListView listView0=(ListView)findViewById(R.id.listView0);
                                      Toast.makeText(getApplicationContext(),"Deleting app : " + listView0.getPositionForView(v),Toast.LENGTH_LONG).show();
                                      iB.setVisibility(View.GONE);
                                      }             
                            }); 
                          Toast.makeText(getApplicationContext(),"Long Clicked on app : " + tv.getText(),Toast.LENGTH_LONG).show();
                          return true;
                         } 
                }); 
           }
           else {
               holder = (ViewHolder) convertView.getTag();
           }

           Appreciation currentApp = appList.get(position);
           holder.text.setText(currentApp.getValue());
           holder.button.setTag(currentApp);

           return convertView;

      }

My problem : when I longClick on an Item, the delete Button appears on another item …

Answer

As the id of the view you want appears multiple times in your list, you need to make sure you get the view with that id that appears in the same list item as your TextView.

If you change

ImageButton editButton = (ImageButton) findViewById(R.id.editButton);

to

ImageButton editButton = (ImageButton) ((ViewGroup) v.getParent()).findViewById(R.id.editButton);

This only searches within the ViewGroup that is the parent of your TextView, so will find the correct instance of R.id.editButton.

UPDATE

Alternatively, you can change

ViewHolder holder = null;

to

final ViewHolder holder;

and then you don’t need to have your variable editButton at all, as you can change the code to

holder.button.setVisibility(View.VISIBLE);

and also call setOnClickListener on holder.button instead of editButton. I’d also move the call to setOnClickListener outside the onLongClick method.

Leave a Reply

Your email address will not be published. Required fields are marked *