如何从自动完成的TextView android的文本 [英] how to get text from autocomplete textview android

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

问题描述

我在我的应用程序,它的工作原理是AutoCompleteTextView。我已经成功地创建了一个onClickItemListener。现在的问题是如何抓住用户选择的文本。这是事情:我有话要传递到适配器搜索建议一个ArrayList。当用户键入一个单词的建议列表变短(行在UI端),所以,当我想从ArrayList中的单词索引的用户选择,我得到了错误的字,因为索引不匹配。我怎样才能获取文本(字符串)用户选择,而不必惹指数?这是我的code:

 公共类AutocompleteActivity扩展BaseActivity {

    私人DBManager m_db;

    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.autocomplete);

        m_db =新DBManager(本);
        最后的ArrayList<字符串>也就是说= m_db.selectAllWords();
        ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(这一点,R.layout.listitem,字);

        AutoCompleteTextView电视=(AutoCompleteTextView)findViewById(R.id.autocomplete);
        tv.setThreshold(1);
        tv.setAdapter(适配器);

        tv.setOnItemClickListener(新OnItemClickListener(){

            公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){
                Log.i(所选文字WAS ------->中,words.get(ARG2));
            }
        });
    }
}
 

解决方案

为arg0 为你的适配器视图 ARG2 的位置。

您是否尝试过:

  arg0.getItemAtPosition(ARG2);
 

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. 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. How can i get the text (String) the user chose without having to mess with the index? Here's my code:

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 being your AdapterView and arg2 the position.

Have you tried:

arg0.getItemAtPosition(arg2);

这篇关于如何从自动完成的TextView android的文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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