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.
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 :)
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
}
});