如何从 AutoCompleteTextView 获取文本?
我的应用程序中有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
是的...不幸的是,您必须实现的 onItemClick 方法上的参数名称不是那么自我描述,但这里有一个示例,其中包含它们的名称:
被单击的 AdapterView(这将是由
适配器)
有关详细信息,请参阅: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:
the AdapterView that was clicked (this will be a view provided by the
adapter)
For more info see: http://developer.android.com/reference/android/widget/AdapterView.OnItemClickListener.html
arg0
是您的AdapterView
,arg2
是位置。你有没有尝试过:
arg0
being yourAdapterView
andarg2
the position.Have you tried:
我想你正在寻找的是这个。
其中
mCountry
是AutoCompleteTextView
。mCountry
是国家/地区列表,我想保存在SharedPreferences
中选择的国家/地区。希望这有帮助。
I think what you are looking for is this.
Where
mCountry
is theAutoCompleteTextView
.mCountry
is the list of countries, and I wanted to save the country selected inSharedPreferences
.Hope this helps.
最简单
要在 AutoCompleteTextView 中获取所选建议的文本,请使用此方法
Easiest of all
For Getting text of the selected suggestion in AutoCompleteTextView use this
试试这个:
try this:
获取在 AutoCompleteTextView 中选择的建议文本的另一种方法是
One another of getting text of suggestion selected in
AutoCompleteTextView
is这是解决问题的代码。
适配器的 getView() 方法中的 SetTag(Dao.getPosition)。
Here is the code that will solve the problem.
SetTag(Dao.getPosition) in getView() method of adapter.
要获取用户选择的显示项目的文本
遵循最佳实践,请为您的变量使用某种形式的描述性命名法,您的代码将更有意义:
To get the text of the displayed item selected by the user
Following best practices, please use some form of descriptive nomenclature for your variables, your code will make more sense:
还有一种方法可以在
onItemClick
之外获取项目:请注意,如果没有下拉菜单或
int getListSelection()
可能会返回ListView.INVALID_POSITION
如果没有选择。There is also a way to get item outside the
onItemClick
:beware that
int getListSelection()
may returnListView.INVALID_POSITION
if there is no dropdown or if there is no selection.对我来说,但在
setOnItemSelectedListener()
: 上就足够了。我想,同样的方法也应该适用于
setOnItemClickListener()
。For me, but on
setOnItemSelectedListener()
:was enough. The same should work on
setOnItemClickListener()
I guess.Kotlin 版本
输出:
Kotlin version
Output: