This question may have been answered on SO several times but it didn't cover what I'm looking for.
I have a listview with a custom adapter. The listview has a textview and a delete ImageView attached to it's row. I want to extract the value of a textview from an item when it's clicked in order to delete that item also from database. I also want to update the list item and I'm using listview.setOnClickListener for that purpose. So I couldn't use the same for delete. I've read about using setTag() and getTag() methods but not sure how to do that exactly. I want to set the textview or rather the string value of the texview as a tag to the delete imageview inside the adapter. Then use getTag() inside the delete.setOnClickListener inside my activity. Could anyone please help me out with this?
Relevant adapter code:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
MyStringReaderHolder holder;
if(row==null)
{
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent,false);
holder= new MyStringReaderHolder();
holder.workLogID = (TextView)row.findViewById(R.id.worklog_id);
holder.delete = (ImageView) row.findViewById(R.id.delete_entry);
row.setTag(holder);
}
else
{
holder=(MyStringReaderHolder) row.getTag();
}
ViewWorkEntryBean mrb = data.elementAt(position);
holder.workLogID.setText(mrb.workLogID);
// mrb.workLogID contains the desired string which I want to pass to delete as a tag
// How do I set the tag?
return row;
}
static class MyStringReaderHolder
{
String billable;
TextView workLogID;
ImageView delete;
}
And this is onClickListener inside activity:
ImageView deleteButton = (ImageView) findViewById(R.id.delete_entry);
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String ID = null;
// how do I get the value of the tag into the string ID
}
});
v.getTag();inside of youronClickgetTagright?ImageViewin your customAdapter class.mrb.workLogIDand pass it to delete as a tag?