如何从 AutoCompleteTextView 获取文本?

发布于 2024-10-14 11:49:47 字数 1208 浏览 2 评论 0原文

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

事情是这样的:我有一个 ArrayList,其中的单词被传递到 Adapter 来搜索建议。当用户输入一个单词时,建议列表会变短(在 UI 侧的行中),因此当我想从用户选择的索引处的 ArrayList 获取单词时,我会得到错误的单词,因为索引不匹配。

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

这是我的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

煮茶煮酒煮时光 2024-10-21 11:49:47

是的...不幸的是,您必须实现的 onItemClick 方法上的参数名称不是那么自我描述,但这里有一个示例,其中包含它们的名称:

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String)parent.getItemAtPosition(position);
        //TODO Do something with the selected text
    }
});
  • parent 单击发生的 AdapterView。
  • 视图 内部视图
    被单击的 AdapterView(这将是由
    适配器)
  • 位置 视图在适配器中的位置
  • id 单击的项目的行 ID。

有关详细信息,请参阅:http://developer.android.com/reference/ android/widget/AdapterView.OnItemClickListener.html

Yeah... unfortunately the name of the parameters on the onItemClick method you must implement are not so self-descriptive but here is an example with the names of what they are:

autoCompleteTextView.setOnItemClickListener(new OnItemClickListener() {
    public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
        String selection = (String)parent.getItemAtPosition(position);
        //TODO Do something with the selected text
    }
});
  • parent The AdapterView where the click happened.
  • view The view within
    the AdapterView that was clicked (this will be a view provided by the
    adapter)
  • position The position of the view in the adapter
  • id The row id of the item that was clicked.

For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html

静若繁花 2024-10-21 11:49:47

arg0 是您的 AdapterViewarg2 是位置。

你有没有尝试过:

arg0.getItemAtPosition(arg2);

arg0 being your AdapterView and arg2 the position.

Have you tried:

arg0.getItemAtPosition(arg2);
萌︼了一个春 2024-10-21 11:49:47

我想你正在寻找的是这个。

String s = this.mCountry.getEditableText().toString();

其中mCountryAutoCompleteTextView

this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
    this.mCountry.setAdapter(adapterCountry);

mCountry 是国家/地区列表,我想保存在 SharedPreferences 中选择的国家/地区。

希望这有帮助。

I think what you are looking for is this.

String s = this.mCountry.getEditableText().toString();

Where mCountry is the AutoCompleteTextView.

this.mCountry = (AutoCompleteTextView) findViewById(R.id.autocomplete_country);
    ArrayAdapter<String> adapterCountry = new ArrayAdapter<String>(this, R.layout.list_item, countries);
    this.mCountry.setAdapter(adapterCountry);

mCountry is the list of countries, and I wanted to save the country selected in SharedPreferences.

Hope this helps.

魂归处 2024-10-21 11:49:47

最简单

要在 AutoCompleteTextView 中获取所选建议的文本,请使用此方法

      autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("========>>", autoCompleteTextView.getText().toString());
        }
    });

Easiest of all

For Getting text of the selected suggestion in AutoCompleteTextView use this

      autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Log.e("========>>", autoCompleteTextView.getText().toString());
        }
    });
自控 2024-10-21 11:49:47

试试这个:

txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
            txtPurpose.setTag(selected);
        }
    });

try this:

txtPurpose.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                                long arg3) {
            Purpose selected = (Purpose) arg0.getAdapter().getItem(arg2);
            txtPurpose.setTag(selected);
        }
    });
方觉久 2024-10-21 11:49:47

获取在 AutoCompleteTextView 中选择的建议文本的另一种方法是

@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{
    TextView txtvw=(TextView) view;
    String str=txtvw.getText().toString();
    int index = contactNames.indexOf(str);
} 

One another of getting text of suggestion selected in AutoCompleteTextView is

@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
{
    TextView txtvw=(TextView) view;
    String str=txtvw.getText().toString();
    int index = contactNames.indexOf(str);
} 
假装爱人 2024-10-21 11:49:47

这是解决问题的代码。

 private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        int index = (int) view.getTag();
        Object item = parent.getItemAtPosition(index);
        if (item instanceof SearchItemShareConnectionDAO) {
            SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;

        }
    }
};

适配器的 getView() 方法中的 SetTag(Dao.getPosition)。

Here is the code that will solve the problem.

 private AdapterView.OnItemClickListener onItemClickListener = new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
        int index = (int) view.getTag();
        Object item = parent.getItemAtPosition(index);
        if (item instanceof SearchItemShareConnectionDAO) {
            SearchItemShareConnectionDAO dao = (SearchItemShareConnectionDAO) item;

        }
    }
};

SetTag(Dao.getPosition) in getView() method of adapter.

送君千里 2024-10-21 11:49:47

要获取用户选择的显示项目的文本

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    String selectedItemText = arg0.getItemAtPosition(arg2);
    Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
}

遵循最佳实践,请为您的变量使用某种形式的描述性命名法,您的代码将更有意义:

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id) 

To get the text of the displayed item selected by the user

public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    String selectedItemText = arg0.getItemAtPosition(arg2);
    Log.i("myTag", "SELECTED TEXT WAS["+selectedItemText+"]);
}

Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int selectedItemIndexIn, long id) 
执笔绘流年 2024-10-21 11:49:47

还有一种方法可以在 onItemClick 之外获取项目:

int index = tv.getListSelection();
if (index != ListView.INVALID_POSITION) {
    Object item = tv.getAdapter().getItem(index);
}

请注意,如果没有下拉菜单或 int getListSelection() 可能会返回 ListView.INVALID_POSITION如果没有选择。

There is also a way to get item outside the onItemClick:

int index = tv.getListSelection();
if (index != ListView.INVALID_POSITION) {
    Object item = tv.getAdapter().getItem(index);
}

beware that int getListSelection() may return ListView.INVALID_POSITION if there is no dropdown or if there is no selection.

笛声青案梦长安 2024-10-21 11:49:47

对我来说,但在 setOnItemSelectedListener(): 上

arg0.getSelectedItem();

就足够了。我想,同样的方法也应该适用于 setOnItemClickListener()

For me, but on setOnItemSelectedListener():

arg0.getSelectedItem();

was enough. The same should work on setOnItemClickListener() I guess.

夜光 2024-10-21 11:49:47

Kotlin 版本

autoCompleteId.setOnItemClickListener { parent, view, position, id ->
            val selectedItem = parent.getItemAtPosition(position).toString()
            Log.d("SELECTED ITEM", selectedItem )
        }

输出:

D/CLASSROOM: selectedItem 

Kotlin version

autoCompleteId.setOnItemClickListener { parent, view, position, id ->
            val selectedItem = parent.getItemAtPosition(position).toString()
            Log.d("SELECTED ITEM", selectedItem )
        }

Output:

D/CLASSROOM: selectedItem 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文