Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onClick listener to a ListView Image - Android

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

like image 605
Fahid Mohammad Avatar asked May 23 '12 07:05

Fahid Mohammad


2 Answers

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 
    }
});
like image 151
C. Leung Avatar answered Oct 11 '22 18:10

C. Leung


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
    }         
}
like image 41
Satheesh Avatar answered Oct 11 '22 17:10

Satheesh