【问题标题】:onItemClick setting text to the AutoCompleteTextViewonItemClick 将文本设置为 AutoCompleteTextView
【发布时间】:2013-11-06 06:29:32
【问题描述】:

我已将 onTextChangedListener 添加到我的自动完成文本视图并使用异步任务填充它

mAutoComplete.addTextChangedListener(new TextWatcher() {
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
        //run an async tast to get autocompletes
    }
    @Override
    public void afterTextChanged(Editable s) {
    }
});

private class getAutoCompletes extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        //get autocompletes
    }
    @Override
    protected void onPostExecute(String result) {
        //create an adapter
        mAdapter AutoCompleteAdapter = new mAdapter(
                mActivity.this,
                R.layout.m_layout,
                R.id.m_id, autocompletesList);
        //set it to the autocomplete textview
        mAutoComplete.setAdapter(AutoCompleteAdapter);
        //show the dropdown
        mAutoComplete.showDropDown();
    }
}

然后我在 mAutoComplete 上有 setOnItemClickListener(new AdapterView.OnItemClickListener() {}。但是什么都不做。

当我单击下拉列表中的任何项目时,我仍然在 mAutoComplete 中将适配器的字符串表示形式作为文本获得

com.xxxx.app.mAdapter@4342ca0

没有我在哪里设置 mAutoComplete 的文本。

编辑:

适配器类:

public class mAdapter extends ArrayAdapter<customDS> {

    private LayoutInflater mInflater = null;
    private Context ctx;
    public ArrayList<customDS> values = new ArrayList<customDS>();

    public mAdapter(Context context, int resource,
            int textViewResourceId, ArrayList<customDS> objects) {
        super(context, resource, textViewResourceId, objects);
        values = objects;
        ctx = context;
        mInflater = (LayoutInflater) ctx
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public int getCount() {
        return values.size();
    }

    public customDS getItem(int position) {
        return values.get(position);
    }

    public long getItemId(int position) {
        return position;
    }

    public static class ViewHolder {
        public TextView title;
        public TextView description;
    }

    public View getView(final int position, View convertView, ViewGroup parent) {

        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.m_layout,
                    parent, false);
            holder.title = (TextView) convertView
                    .findViewById(R.id.m_id);
            holder.description = (TextView) convertView
                    .findViewById(R.id.m_id2);

            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.title.setText(values.get(position).title);
        holder.description.setText(values.get(position).description);

        return convertView;
    }
}

【问题讨论】:

  • customDS 对象的外观如何?在此处添加它的源代码。

标签: android


【解决方案1】:

您可以创建 AutoCompleteTextView 的子类并覆盖“replaceText”方法,因为在它的超类 (AutoCompleteTextView) 中,“replaceText”用于在单击结果时替换视图中的当前文本。

public class CustomAutoCompleteTextView extends AutoCompleteTextView {
    public CustomAutoCompleteTextView(Context context) {
        super(context);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomAutoCompleteTextView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    protected void replaceText(CharSequence text) {
        // do nothing so that the text stays the same
    }
}

【讨论】:

  • 看起来很 hacky,但仍然可能是一种解决方法,前提是当前对象的 toString() 实现包含您真正想要显示的内容...
  • 有点,但除了做一些完全自定义的事情之外,在阅读 AutoCompleteTextView 源代码后,这似乎是一个不错的解决方案。我自己也用了一段时间,它似乎表现不错。
  • 查看其他解决方案的 cmets。还有更好的解决方案。
  • 更好的解决方案有点主观。我以前见过其他解决方案,但不喜欢它(通过文本观察器保存和恢复文本等)。看到我的用例基本上不需要 replaceText 功能,似乎适合让它什么都不做。我认为这两种方式都有效。真正反对我建议的唯一好的论点是在某个时候更改 API / 父类。
  • 我也相信 AutoCompleteTextView 依赖于你的对象的 toString 来达到它的目的 IIRC
【解决方案2】:

我仍然将适配器的字符串表示形式作为文本 mAutoComplete,当我点击下拉列表中的任何项目时

这是因为当您从下拉列表中选择一个项目时,自动完成小部件将调用toString() 方法来填充插入输入的EditText。

尝试覆盖customDS 类的toString() 方法,以从对象返回您希望在该处看到的内容。

【讨论】:

  • 我不想设置任何文本,我希望文本是输入的内容。
  • @Archie.bpgc 您是否希望用户选择一个项目但保留之前在EditText 中输入的文本?这对于处理自动完成小部件的用户来说是非常反直觉的。
  • 下拉菜单有不同类型的项目。对于特定类型的项目,如果用户选择它,则会出现一个弹出窗口,要求 Proceed/Cancel 等取消我希望文本保持以前的状态(仅用户键入的内容)。跨度>
  • @Archie.bpgc 这对用户来说听起来也很违反直觉(因此您可能希望让用户选择他/她想要的内容并让它使用按钮更进一步(请求许可) )。但是,没有什么可以阻止您将用户输入的文本存储在某个变量中(在 TextWatcher 中),然后使用它将小部件恢复到初始状态。
  • @XtremeBiker 覆盖AutoCompleteTextView 的convertSelectionToString() 方法并返回一个CharSequence,表示您想要的所选对象的任何部分。默认情况下,该方法在所选项目上调用 toString()。
【解决方案3】:

这更简单。尝试将其添加到您的活动中。它对我有用。

mAutoComplete.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        mAutoComplete.setText(((TextView) view).getText());

        // Or maybe you need to do something like this (it depends from your R.layout.m_layout):
        // LinearLayout l = (LinearLayout) view;
        // TextView t = (TextView) l.getChildAt(0);
        // mAutoComplete.setText(t.getText());
    }
});

【讨论】:

    【解决方案4】:

    除了jaredpetker的解决方案:

    @Override
        protected void replaceText(CharSequence text) {
            Editable currentText = getText();
            super.replaceText(currentText);
        }
    

    【讨论】:

      【解决方案5】:

      你可以简单地做 .setText("");在其 onItemClick 中避免 AutocompleteTextView 上的任何设置文本。下面是例子:

        private AdapterView.OnItemClickListener onItemClickListener =
              new AdapterView.OnItemClickListener(){
                  @Override
                  public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                      query.setText("");
                      UsersVO vo= (UsersVO)adapterView.getItemAtPosition(i);
                      Intent intent=new Intent(ActivityA.this, ActivityB.class);
                      intent.putExtra("NAME",vo.getName());
                      intent.putExtra("USER",vo.getUser());
                      startActivity(intent);
                  }
              };
      

      查询是 autoCompleteTextView。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-02-02
        • 1970-01-01
        • 1970-01-01
        • 2011-02-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多