我有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。
我已經處理ImageView的的的onClick監聽器。
很難獲取正在饋入適配器的數據的id
。
我可以使用該ImageView刪除適配器的數據嗎?
根據建議如何在ImageView的標籤中設置Cursor ID?由於光標將CursorIndexOutOfBoundException
傳遞給MyCustomAdapter
時
沒有什麼從傳遞作爲標記爲阻止你的'ImageView'的標識來自'Cursor'(在'getView()'方法中)。然後你可以用'v.getTag()'在'onClick()'中檢索它。 – Luksprog
@Luksprog:你指導聲音的最佳途徑。但我沒有得到如何設置從光標到該ImageView的標籤。 –
在自定義適配器的'getView()'(或'bindView()')方法中,從'Cursor'中檢索id。將該id設置爲「ImageView」,「imageView.setTag(Long.valueOf(idFromCursor))」的標記。 – Luksprog