onItemClick设置文本到AutoCompleteTextView

时间:2013-11-06 06:29:33

标签: android

我已将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;
    }
}

5 个答案:

答案 0 :(得分:18)

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

答案 1 :(得分:7)

  

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

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

尝试覆盖toString()类的customDS方法,以便从对象返回您想要查看的内容。

答案 2 :(得分:4)

这更简单。尝试在您的活动中添加此内容。它对我有用。

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

答案 3 :(得分:1)

除了jaredpetker的解决方案:

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

答案 4 :(得分: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的地方。