Question

I have AutoCompleteTextView which uses to search the value from database. On Click of filtered value it's set to the AutoCompleteTextView which can be use to update the value for the particular data.

I would like to incorporate delete ImageView functionallity next to filtered item. On Click of it Alert Dialog whether to delete or not. Been able to develop the scenario.

MyCursorAdapter adapter = new MyCursorAdapter(this, R.layout.edt_delete_item, null, fromName, to);
searchText.setAdapter(adapter);


adapter.setCursorToStringConverter(new CursorToStringConverter() {
            @Override
            public String convertToString(android.database.Cursor cursor) {
                // Get the label for this row out of the "state" column
                //final int columnIndex = cursor.getColumnIndexOrThrow("state");
                int index = cursor.getColumnIndex(DBConstant.Patient_Name_Columns.COLUMN_NAME);
                String strName = "";
                if(index != -1)
                {
                    strName = cursor.getString(index);
                }
                return strName;
            }
        });

QueryFilter has been used on Custom Adapter:-

adapter.setFilterQueryProvider(new FilterQueryProvider() {
public Cursor runQuery(CharSequence constraint) {
    Cursor cursor = getContentResolver().query(DBConstant.Patient_Name_Columns.CONTENT_URI, null,DBConstant.Patient_Name_Columns.COLUMN_NAME_SEARCHALGO + " like '%" + SearchAlgo.getNameSearchAlgo(constraint.toString())+"%'", null, "0");
                return cursor;
            }
        });

Custom Adapter:-

public class MyCursorAdapter extends SimpleCursorAdapter{
  public MyCursorAdapter(Context context, int layout, Cursor c,
        String[] from, int[] to) {
    super(context, layout, c, from, to);
}  

@Override 
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
    if(position % 2 == 0){  
        view.setBackgroundColor(Color.rgb(238, 233, 233));
    }
    else {
        view.setBackgroundColor(Color.rgb(255, 255, 255));
    }
    return view;  
    }  
 }

Adapter of AutoCompleteTextView has layout as below inside edt_delete_item having ImageView with delete option.

On Click of Adapter it get set in AutoCompleteTextView -> SearchText.

What UIx looks like

I've already handled onClick Listener of ImageView.

It's hard to get the id of the data which is feeding in the adapter.

Can i delete the data of adapter with that ImageView?

As per suggestion how one can set the Cursor ID in the tag of ImageView? As cursor throws CursorIndexOutOfBoundException when passing it to MyCustomAdapter

Was it helpful?

Solution

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.
     }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top