Reputation: 930
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 :)
Upvotes: 16
Views: 22812
Reputation: 1730
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
}
}
Upvotes: 3
Reputation: 6318
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
}
});
Upvotes: 9
Reputation: 458
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 !!
Upvotes: 2