如何从AutoCompleteTextView带适配器直接删除数据 [英] how to delete data from AutoCompleteTextView with adapter directly

查看:288
本文介绍了如何从AutoCompleteTextView带适配器直接删除数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有<大骨节病> AutoCompleteTextView 其中用于搜索从数据库中的值。在点击过滤值它设置为<大骨节病> AutoCompleteTextView 它可以使用更新的特定数据的价值。

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.

我想结合删除<大骨节病>的ImageView functionallity旁边的过滤项目。在点击它警告对话框是否删除与否。之所以能够发展的方案。

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已使用的自定义适配器: -

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

的<大骨节病> AutoCompleteTextView 适配器有布局如下里面 edt_delete_item 有<大骨节病>的ImageView 与删除选项。

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

在单击适配器的它被置<大骨节病> AutoCompleteTextView - > SEARCHTEXT

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

我已经处理了的onClick监听器<大骨节病>的ImageView

I've already handled onClick Listener of ImageView.

这是很难获得 ID 这是在适配器喂养的数据。

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

我可以说的ImageView删除适配器的数据?

Can i delete the data of adapter with that ImageView?

根据建议,一个人如何能的ImageView的标签设置光标ID?随着光标抛出 CursorIndexOutOfBoundException 将它传递给 MyCustomAdapter

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

推荐答案

作为建议的Luksprog setTag getTag 是实现我想要的目标的道路。在 getView设置标签()内&LT;大骨节病&GT;的ImageView&LT; /万桶方式&gt; 和获取标签背面onClick事件是执行操作的正确选择。

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.

变更 getView() CustomAdapter延伸 SimpleCursorAdapter

Changed getView() of CustomAdapter which extends SimpleCursorAdapter.

code片断: -

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的<大骨节病>的ImageView 处理的删除选项: -

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

这篇关于如何从AutoCompleteTextView带适配器直接删除数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆