如何从AutoCompleteTextView获取文本? [英] How to get text from AutoCompleteTextView?

查看:282
本文介绍了如何从AutoCompleteTextView获取文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序中有一个 AutoCompleteTextView 可以正常工作。我已经成功创建了 onClickItemListener 。问题是如何获取用户选择的文本。

I have an AutoCompleteTextView in my app which works. I have successfully created an onClickItemListener. The question is how to grab the text the user selected.

这就是问题:我有一个 ArrayList 并将单词传递给 Adapter 来搜索建议。当用户键入单词时,建议列表会变短(在UI端的行中),因此当我想从 ArrayList 的索引处获取单词时,用户选择了i

And this is the thing: I have an ArrayList with words being passed to the Adapter to search for suggestions. As the user types a word the suggestions list gets shorter (in rows on the UI side) so when i want to get the word from the ArrayList at the index the user selected i get the wrong word because the indexes doesn't match.

如何获取文本( String )用户选择而不必弄乱索引?

How can I get the text (String) the user chose without having to mess with the index?

这是我的代码:

public class AutocompleteActivity extends BaseActivity {

    private DBManager m_db;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.autocomplete);

        m_db = new DBManager(this);
        final ArrayList<String> words = m_db.selectAllWords();
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.listitem, words);

        AutoCompleteTextView tv = (AutoCompleteTextView)findViewById(R.id.autocomplete);
        tv.setThreshold(1);
        tv.setAdapter(adapter);

        tv.setOnItemClickListener(new OnItemClickListener() {

            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
                Log.i("SELECTED TEXT WAS------->", words.get(arg2));
            }
        });
    }
}


推荐答案

arg0 是您的 AdapterView arg2 的职位。

您是否尝试过:

arg0.getItemAtPosition(arg2);

这篇关于如何从AutoCompleteTextView获取文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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