As suggested by Luksprog setTag
and getTag
is the way to achieve the goal i want. Setting Tag in getView() within <kbd>ImageView</kbd>
and getting Tag back onClick event is the right choice to perform the operation.
Changed getView()
of CustomAdapter which extends SimpleCursorAdapter
.
Code snippet:-
public View getView(int position, View convertView, ViewGroup parent) {
// get reference to the row
View view = super.getView(position, convertView, parent);
// check for odd or even to set alternate colors to the row background
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.edt_delete_item, null);
getCursor().moveToPosition(position);
long id = getCursor().getLong(getCursor().getColumnIndex(DBConstant.Patient_Name_Columns.COLUMN_ID));
TextView name = (TextView)view.findViewById(R.id.txtText);
ImageView delete = (ImageView) view.findViewById(R.id.deleteIcon);
String strName = getCursor().getString(getCursor().getColumnIndex(DBConstant.Patient_Name_Columns.COLUMN_NAME));
name.setText(strName);
delete.setTag(String.valueOf(id));
return view;
}
OnClickListener of ImageView Handled the delete option:-
boolean d = false;
String _id = v.getTag(); //v is the view in here i.e ImageView in my case.
d= SmartConsultant.getApplication().getContentResolver().delete(DBConstant.Patient_Name_Columns.CONTENT_URI, "_id=?", new String[] { _id }) > 0;
if(d)
{
//Show Toast Successfully deleted.
}