1

我有AutoCompleteTextView它用於從數據庫中搜索值。在點擊篩選值時,它被設置爲AutoCompleteTextView可用於更新特定數據的值。如何使用適配器直接從AutoCompleteTextView中刪除數據

我想加入刪除ImageView functionallity旁邊的過濾項目。點擊它警報對話框是否刪除或不刪除。能夠開發該場景。

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已被用於對自定義適配器: -

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; 
      } 
     }); 

定製適配器: -

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; 
    } 
} 

AutoCompleteTextView的適配器有佈局如下內部edt_delete_item具有ImageView的與刪除選項。

在適配器上單擊它將被設置在AutoCompleteTextView - > SearchText。

What UIx looks like

我已經處理ImageView的的的onClick監聽器。

很難獲取正在饋入適配器的數據的id

我可以使用該ImageView刪除適配器的數據嗎?

根據建議如何在ImageView的標籤中設置Cursor ID?由於光標將CursorIndexOutOfBoundException傳遞給MyCustomAdapter

+0

沒有什麼從傳遞作爲標記爲阻止你的'ImageView'的標識來自'Cursor'(在'getView()'方法中)。然後你可以用'v.getTag()'在'onClick()'中檢索它。 – Luksprog

+0

@Luksprog:你指導聲音的最佳途徑。但我沒有得到如何設置從光標到該ImageView的標籤。 –

+0

在自定義適配器的'getView()'(或'bindView()')方法中,從'Cursor'中檢索id。將該id設置爲「ImageView」,「imageView.setTag(Long.valueOf(idFromCursor))」的標記。 – Luksprog

回答

1

正如Luksprog setTaggetTag所建議的那樣是實現我想要的目標的方法。在getView() within <kbd>ImageView</kbd>中設置標籤並獲取標記回onClick事件是執行該操作的正確選擇。

更改getView() CustomAdapter其擴展SimpleCursorAdapter

代碼片段: -

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 的ImageView的處理的刪除選項: -

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. 
    } 
+0

由於未在自定義適配器的getView()中設置標籤,因此我得到'NullPointerException'。我用newView而不是getView,但沒有這樣的區別。請告訴我我做錯了什麼? –

+1

@VikalpPatel對不起,我的意思是**'bindView()'**方法,'newView()'方法不會爲每一行調用。我不知道爲什麼拋出異常,你在哪裏爲ImageView設置了監聽器? – Luksprog

相關問題