AutoCompleteTextView检测用户是否选择了建议

时间:2011-11-15 10:49:52

标签: android android-adapter

如果他们按下了我的autocompleteTextview中的建议,我只是不希望用户能够点击按钮。

为了实现这一点,我在我的适配器上实现了一个onKeyListener并删除了适配器设置的标签,如果你按下了一个建议。然后我检查了是否有标签。

但onKeyListener似乎没有正确删除标记:

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

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

3 个答案:

答案 0 :(得分:8)

查看您的代码:

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

  • setTag(null)从侦听器的OnKey()方法调用,当用户点击键盘键时将调用该方法。我也认为这不对。

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

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

答案 1 :(得分:6)

您可以使用自动完成文本视图的setOnClickListner方法。当列表项获得焦点时,项目选择的侦听器将触发。不是点击(选择)项目。

//set on click listener on the auto complete text view
autoField.setOnItemClickListener(autoItemSelectedListner);

然后,

private OnItemClickListener autoItemSelectedListner = new OnItemClickListener()
{
    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
    {
        //extract selected
        selected = adapter.getItem(arg2);
    }
};

我希望这有帮助..

答案 2 :(得分:-3)

嗯,它不明白你为什么使用这个标签。也许我错过了一个约束。 否则,我认为这就是你要追求的目标:

autoCompleteTextView.setOnItemSelectedListener(new OnItemSelectedListener() {
  @override
  void public onItemSelected(AdapterView<?> parent, View view, int position, long id) {
     makeSpecialButtonAvailable();
  }
  @override
  void public onNothingSelected(AdapterView<?> parent) {
    //ignore
  }


});