how to delete data from AutoCompleteTextView with adapter directly

余生颓废 提交于 2019-12-08 19:10:40
Luksprog

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.
     }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!