gpt4 book ai didi

android - 如何直接使用适配器从 AutoCompleteTextView 中删除数据

转载 作者:行者123 更新时间:2023-11-30 03:10:18 25 4
gpt4 key购买 nike

我有 AutoCompleteTextView 用于从数据库中搜索值。单击过滤值时,它会设置为 AutoCompleteTextView,可用于更新特定数据的值。

我想在过滤项目旁边加入删除 ImageView 功能。单击它时,警告对话框是否删除。能够开发场景。

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?由于游标在将其传递给 MyCustomAdapter

时抛出 CursorIndexOutOfBoundException

最佳答案

根据 Luksprog 的建议 setTaggetTag是实现我想要的目标的方法。在 getView() within <kbd>ImageView</kbd> 中设置标记在点击事件中获取标签返回是执行操作的正确选择。

已更改 getView()扩展 SimpleCursorAdapter 的 CustomAdapter .

代码 fragment :-

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

ImageView 的 OnClickListener 处理了删除选项:-

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

关于android - 如何直接使用适配器从 AutoCompleteTextView 中删除数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21111519/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com