AutoCompleteTextView检测用户是否选择了建议 [英] AutoCompleteTextView detect if user choosed suggestion

查看:68
本文介绍了AutoCompleteTextView检测用户是否选择了建议的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果用户按下了我的autocompleteTextview中的建议,我将只希望用户单击按钮.

I only wan't the user to be able to click on a button if they have pressed a suggestion from my autocompleteTextview.

要实现此目的,我在适配器上实现了onKeyListener,如果您按了建议,则删除了由适配器设置的标记.然后我检查是否有标签.

To accomplish this, i implemented an onKeyListener im my adapter and removed the tag which was set by the adapter if you pressed a suggestion. Then i checked if there was tag.

但是onKeyListener似乎无法正确删除标记:

But the onKeyListener does not seem to remove the tag properly:

public class StopCursorAdapter extends CursorAdapter{

    private Context context;
    private LayoutInflater inflater;
    private AutoCompleteTextView autoCompleteTextView;

    public StopCursorAdapter(final AutoCompleteTextView autoCompleteTextView, Context context, Cursor c){
        super(context, c);
        this.context = context;
        this.autoCompleteTextView = autoCompleteTextView;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        this.autoCompleteTextView.setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event){
                StopCursorAdapter.this.autoCompleteTextView.setTag(null);
                Log.d("cursor", "Removed tag");
                Log.d("cursor", String.valueOf(StopCursorAdapter.this.autoCompleteTextView.getTag() == null));
                Log.d("cursor", String.valueOf(autoCompleteTextView.getTag() == null));         
                return false;
            }
        });
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent){
        View v = inflater.inflate(android.R.layout.two_line_list_item, null);
        return v;
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor){
        TextView txt1 = (TextView) view.findViewById(android.R.id.text1);
        TextView txt2 = (TextView) view.findViewById(android.R.id.text2);

        txt1.setTextColor(Color.BLACK);
        txt1.setText(cursor.getString(2));
        txt2.setText(cursor.getString(3));
    }

    @Override
    public CharSequence convertToString(Cursor cursor){
        autoCompleteTextView.setTag(new Stop(cursor.getString(1), cursor.getString(2), cursor
                .getString(3)));
        return cursor.getString(2);// + ", " + cursor.getString(3);
    }

    @Override
    public Cursor runQueryOnBackgroundThread(CharSequence constraint){
        Database.getInstance().ensureLoaded(context);
        String filter = "";
        if(constraint == null){
            filter = "";
        }else{
            filter = constraint.toString();
        }
        Cursor cursor = Database.getInstance().getStopsCursor(filter);
        return cursor;
    }
}

还有其他方法可以解决此问题吗?

Is there any other way to solve this issue?

推荐答案

查看您的代码:

    从适配器的convertToString()方法中调用
  • setTag(new Stop(...)),该方法将在构建选择列表(针对游标中的每一行)以及执行完成时由AutoCompleteTextView调用.我认为这不是您想要的.

  • setTag(new Stop(...)) is called from the adapter's convertToString() method which will be called by the AutoCompleteTextView when it is building the selection list (for each row in the cursor) and also when it is performing the completion. I don't think this is what you want.

setTag(null)是通过侦听器的OnKey()方法调用的,当用户点击键盘按键时,该方法将被调用.我也不认为这是正确的.

setTag(null) is called from the listener's OnKey() method which will be called when the user tap on the keyboard keys. I also don't think this is correct.

我认为正确的代码应该与此类似:

I think the correct code should be something similar to this instead:

    // set tag to non-null when key is pressed
    autoCompleteTextView.setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            autoCompleteTextView.setTag(new Stop());
            return false;
        }
    });

    // set tag to null when an item is tapped
    autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> p, View v, int pos, long id) {
            autoCompleteTextView.setTag(null);
        }
    });

这篇关于AutoCompleteTextView检测用户是否选择了建议的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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