onItemClick将文本设置为AutoCompleteTextView [英] onItemClick setting text to the AutoCompleteTextView

查看:55
本文介绍了onItemClick将文本设置为AutoCompleteTextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在我的自动完成文本视图中添加了onTextChangedListener,并使用异步任务填充了它

I have added a onTextChangedListener to my autocomplete textview and populate it using an async task

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(){} .但是什么也没做.

Then I have setOnItemClickListener(new AdapterView.OnItemClickListener() {} on the mAutoComplete. But doing nothing in it.

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

com.xxxx.app.mAdapter@4342ca0

否,我要在其中设置mAutoComplete的文本.

No where I am setting the text for the 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;
    }
}

推荐答案

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

Still I get the String representation of the adapter as text in the mAutoComplete, when I click on any item in the dropdown

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

That's because when you select an item from drop down the auto complete widget will call the toString() method to fill the EditText where the input is inserted.

尝试覆盖 customDS 类的 toString()方法,以返回您希望从对象那里看到的内容.

Try overriding the toString() method of the customDS class to return what you want to see there from the object.

这篇关于onItemClick将文本设置为AutoCompleteTextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆