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

然后我setOnItemClickListener(new AdapterView.OnItemClickListener() {}在 mAutoComplete 上。但在其中什么也不做。

当我单击下拉列表中的任何项目时,我仍然在 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;
    }
}
4

5 回答 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
    }
}
于 2014-05-22T22:03:45.667 回答
8

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

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

尝试覆盖类的toString()方法customDS以从对象返回您想在那里看到的内容。

于 2013-11-06T07:47:49.957 回答
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());
    }
});
于 2015-06-13T09:39:35.927 回答
1

除了 jaredpetker 的解决方案:

@Override
    protected void replaceText(CharSequence text) {
        Editable currentText = getText();
        super.replaceText(currentText);
    }
于 2016-05-20T12:02:44.983 回答
0

你可以简单地做 .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。

于 2018-11-13T09:35:14.543 回答