onClick listener to a ListView Image - Android

22,317

Solution 1

You should do it In your Adapter not in Activity.

yourImg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // things you want to do 
    }
});

Solution 2

Create one interface for communicating from adapter to activity

public interface ListenerActivity        
{    
public void Remove();    
}

In Your Adapter Class:

class CustomAdapter extends ArrayAdapter<String> {

ListenerActivity listener;

CustomAdapter(YourActivity obj) {
  super(Activity.this, R.layout.row, R.id.label, items);

  listener=obj;
}
 public View getView(final int position, View convertView,
                ViewGroup parent) {
ImageId.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        listener.Remove();
    }
});
}

In Your Activity:

class MyActivity extends Activity implements ListenerActivity
{
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}
 @Override
    public void Remove() {

//Do your remove functionality here
    }         
}

Solution 3

Adding to the last answer and reering to this answer, I added some code to refresh the view after deleting an item or others actions:

//in your adapter you have to get list of items:

 public CartListAdapter(Context context, int resourceId,
        List<CartItemModel> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items= items;
}

//after that you have to set a listner on the imageView like this:

public View getView(int position, View convertView, ViewGroup parent) {
   holder.imageViewDelete.setOnClickListener(new imageViewClickListener(position));
   // some other lines code ...
   // here we get the item to remove
    rowItem= getItem(position);
    }

//and here the definition of the listner:

private class imageViewClickListene implements OnClickListener {
   int position;
    public imageViewClickListene( int pos)
        {
            this.position = pos;
        }

    public void onClick(View v) {

    // here we  remove the selected item    
        items.remove(rowItem);
            // here we refresh the adapter
        CartListAdapter.this.notifyDataSetChanged();

}
}

It works for me, enjoy !!

Share:
22,317
Fahid Mohammad
Author by

Fahid Mohammad

Learner...

Updated on February 17, 2020

Comments

  • Fahid Mohammad
    Fahid Mohammad over 4 years

    I have a ListView with an image over the right hand side. and I wanted to perform a onClick listener event by clicking the image on the ListView. Please see the image for reference.

    enter image description here

    I know basic OnClick listener Implementations, but this seems to be a little tricky to me :P

    Forgot to mention, by clicking the actual ListView will shootup a new activity, so I need to maintain both the schemas.

     listView.setOnItemClickListener(new OnItemClickListener() {
    
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position,
                    long id) {
                eventsData.remove(id);
                cursor.requery(); 
            }
    
        });   
    

    The code above perform a deletion by clicking on any list element eventsData.remove(id); is a database helper for executing this task. like I said now I need a method to perform this same process juts by clicking the image, not the entire list element, I want the list element to do some other action later on.

    I hope now I'm clear a bit.


    The Solution:

    If anybody come across the same sort of situation then here is the complete code for the adapter.

     class CustomAdapter extends ArrayAdapter<String> {
     CustomAdapter() {
      super(Activity.this, R.layout.row, R.id.label, items);
    }
    
    public View getView(final int position, View convertView,
                        ViewGroup parent) {
      View row=super.getView(position, convertView, parent);
      deleteImg=(ImageView)row.findViewById(R.id.icon);
    
      deleteImg.setImageResource(R.drawable.delete);      
      deleteImg.setOnClickListener(new OnClickListener() {
          String s = items[position];
        @Override
        public void onClick(View v) {
            Toast.makeText(context, s, Toast.LENGTH_SHORT).show();          
        }
    });
    
      return(row);
    }
    

    }

    I know the coding is a bit crappy so bear with me, I just want to show the actual process that's it.

    It's working for me :)

  • Fahid Mohammad
    Fahid Mohammad almost 12 years
    How do i determine the current state of the list element?? any pointer or identifier??
  • C. Leung
    C. Leung almost 12 years
    what do you mean by "current state"?
  • Fahid Mohammad
    Fahid Mohammad about 10 years
    Nice one @Satheesh :) +1 ;)