I have a ListView which is populated using a web service. I want to delete a particular entry from the ListView For that I need to get an ID (a texview field), one of the elements from the ListView and pass it to the database. I have an ImageView (a red cross) for every list element which needs to be clicked in order to delete that entry. All I want is to get the value of the text field when I click on the delete image of the same list element. I used an android:onClick xml attribute to call a function deleteEntry. But I’m not sure how to get a particular value of the ID which is in the same list element as the delete image. How can I do that?
EDIT
This is my adapter:
public class MyCustomAdapterWorkEntry extends ArrayAdapter<ViewWorkEntryBean> {
Context context;
int layoutResourceId;
ViewWorkEntryBean currentMRB;
Vector<ViewWorkEntryBean> data;
public MyCustomAdapterWorkEntry(Context context, int layoutResourceId, Vector<ViewWorkEntryBean> data)
{
super(context,layoutResourceId,data);
this.layoutResourceId = layoutResourceId;
this.context=context;
this.data = data;
}
@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);
row.setTag(holder);
}
else
{
holder=(MyStringReaderHolder) row.getTag();
}
ViewWorkEntryBean mrb = data.elementAt(position);
holder.workLogID.setText(mrb.workLogID);
holder.delete.setTag(mrb.workLogID);
return row;
}
static class MyStringReaderHolder
{
TextView workLogID;
ImageView delete;
}
}
And this is the delete function:
public void DeleteWorkEntryClick(View v) {
String workLogID = null;
workLogID = (String) v.getTag();
deleteWorkEntry(workLogID);
}
It still doesn’t work! workLogID is null.
Set the specific ID value you need to pass to the function as the tag value of each “X” button in the list using
setTag(). That way, when the view clicked is passed to youronClick()method, you can callview.getTag()to obtain that number again and pass it to your DB function.