在AutoCompleteTextView中设置文本的onItemClick

6
我向我的自动完成文本视图添加了一个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对象是什么样子?在这里添加它的源代码。 - Simon Dorociak
5个回答

20

你可以创建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
    }
}

更好的解决方案有点主观。我之前看过另一种解决方案,但并不喜欢它(通过文本监视器等保存和恢复文本)。考虑到在我的用例中实际上不需要replaceText功能,所以让它什么也不做似乎是合适的。我认为两种方法都是有效的。真正反对我提出的建议的唯一好论据可能是在某个时候更改API/父类。 - jaredpetker
我认为 AutoCompleteTextView 依赖于你的对象的 toString 方法以使其正常工作。如果我没记错的话。 - jaredpetker
The tostring方法在我看来与应用程序的最终客户无关。如果你看一下《Effective Java》这本书(Joshua Bloch),你会发现它更多地与文档有关。无论如何,解析字符串比覆盖convertSelectionToString方法要不舒服得多,后者接受一个对象作为参数并返回一个字符串。 - Aritz
我不太明白你的陈述意图,也不想就此争论,你可以按照自己的喜好去做,但是无论哪种解决方案都可能达到你所期望的最终结果。祝你好运,玩得开心。 - jaredpetker
@Override protected void replaceText(CharSequence text) { Editable currentText = getText(); super.replaceText(currentText); } - Roman M
显示剩余3条评论

8
当我从下拉菜单中选择项目时,在mAutoComplete中仍然会以文本形式获得适配器的字符串表示形式。这是因为当您从下拉列表中选择一个项目时,自动完成小部件将调用toString()方法来填充插入输入的EditText。
尝试覆盖customDS类的toString()方法,以从对象返回您想在那里看到的内容。

1
我不想设置任何文本,我希望文本是已输入的内容。 - Archie.bpgc
@Archie.bpgc 你想让用户选择一个项目,但保留之前在“EditText”中输入的文本吗?这对于用户处理自动完成小部件来说非常反直觉。 - user
下拉菜单有不同类型的项目。对于特定类型的项目,如果用户选择它,则会弹出一个窗口询问是否“继续/取消”,因此在取消时,我希望文本保持与先前相同(仅保留用户输入的内容)。 - Archie.bpgc
@Archie.bpgc,这对用户来说也非常违反直觉(因此您可能希望让用户选择他/她想要的,并使用按钮继续(请求权限))。但是,没有任何阻止您在某个变量中存储用户输入的文本(在TextWatcher中),然后稍后使用它将小部件恢复到初始状态。 - user
3
覆盖AutoCompleteTextViewconvertSelectionToString()方法,并返回表示所选对象任何部分的CharSequence。默认情况下,该方法调用所选项目上的toString()方法。 - user
显示剩余2条评论

5

这个更简单。尝试在你的活动中添加这个。我已经测试过了。

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

1
除了jaredpetker的解决方案之外:

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

0
您可以在onItemClick中使用.setText("");来避免在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。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接