2

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

Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • There's nothing stopping you from passing as the tag for the `ImageView` the id of the row from the `Cursor`(in the `getView()` method). You can then retrieve it in `onClick()` with `v.getTag()`. – user Jan 14 '14 at 10:50
  • @Luksprog : Where you guiding sounds perfect way to go. But I'm not getting how to set the tag from cursor to that ImageView. – Vikalp Patel Jan 14 '14 at 10:58
  • In the `getView()`(or `bindView()`) method of your custom adapter retrieve the id from the `Cursor`. Set that id as a tag for the `ImageView`, `imageView.setTag(Long.valueOf(idFromCursor))`. – user Jan 14 '14 at 11:01
  • @Luksprog : I reached till where you guiding but still I've doubt how to pass `cursor id` to custom adapter.I'm appending my custom adapter in OP.can you show small snippet of code? – Vikalp Patel Jan 14 '14 at 11:11

1 Answers1

1

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.
     }
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
user
  • 86,916
  • 18
  • 197
  • 190
  • I'm getting `NullPointerException` as Tag has not been set in `getView()` of Custom Adapter. I've used newView instead of getView but there's no such difference. Could please tell me what I'm doing wrong? – Vikalp Patel Jan 14 '14 at 13:40
  • 1
    @VikalpPatel Sorry, I meant the **`bindView()`** method, the `newView()` method is not called for each row. I don't know why that exception is thrown, where do you set the listener for the `ImageView`? – user Jan 14 '14 at 13:46