3

如果用户从我的 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;
    }
}

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

4

3 回答 3

9

查看您的代码:

  • 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);
        }
    });
于 2011-12-09T01:44:58.713 回答
7

您可以使用自动完成文本视图的 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);
    }
};

我希望这有帮助..

于 2011-12-11T17:48:16.953 回答
-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
  }


});
于 2011-12-06T21:01:27.190 回答