gpt4 book ai didi

java - Android - AutoCompleteTextView、showDropDown() 并不总是有效

转载 作者:行者123 更新时间:2023-12-02 07:14:17 26 4
gpt4 key购买 nike

当我每次尝试动态更新 autoCompleteTextview ArrayAdapter 时,如果显示下拉列表,该下拉列表就会消失并且 showDropDown() 不起作用。

我的代码:

    @Override
protected void onCreate(Bundle savedInstanceState) {

.................................................

mAutoComepleteTextView = (AutoCompleteTextView)findViewById(R.id.tv_autocomplete_search);
mAutoComepleteTextView.setOnItemClickListener(this);
mAutoComepleteTextView.addTextChangedListener(this);

mArrayAdaptor = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
mArrayAdaptor.setNotifyOnChange(false);

mAutoComepleteTextView.setAdapter(mArrayAdaptor);
mAutoComepleteTextView.setOnFocusChangeListener(this);
mAutoComepleteTextView.setOnClickListener(this);
.................................................
}

OnClick 事件:

   @Override
public void onClick(View v) {
super.onClick(v);
switch(v.getId()){
case R.id.tv_autocomplete_search:
mAutoComepleteTextView.showDropDown(); // here work fine
break;
default :
break;
}
}

onFocusChange 事件:

    @Override
public void onFocusChange(View v, boolean hasFocus) {
switch(v.getId()){
case R.id.tv_autocomplete_search:
mAutoComepleteTextView.showDropDown(); // here work fine
break;
default :
break;
}
}

我的回调,这是我的问题:

    @Override
public void onNewFriends(Vector<Friend> friends) {
super.onNewFriends(friends);
runOnUiThread(new Runnable() {

@Override
public void run() {
boolean isVisible = mAutoComepleteTextView.isPopupShowing();
if(isVisible)
mAutoComepleteTextView.dismissDropDown();
mArrayAdaptor.clear();

for (Friend friend : ActivitiesPrefsData.getInstace().getFriends()) {
if (friend.m_strEmail.equals(D.username)){
mArrayAdaptor.add(friend.m_deviceInfo.m_strName);
}
else
mArrayAdaptor.add(friend.m_strEmail);
}
mArrayAdaptor.notifyDataSetChanged();
if(isVisible)
mAutoComepleteTextView.showDropDown(); // here doesn't work
}
});
}

如果有任何帮助,我将不胜感激,谢谢。

最佳答案

我在这里发布了我找到的解决方案,重置适配器:

    @Override
public void onNewFriends(Vector<Friend> friends) {
super.onNewFriends(friends);
runOnUiThread(new Runnable() {

@Override
public void run() {
mArrayAdaptor.clear();

for (Friend friend : ActivitiesPrefsData.getInstace().getFriends()) {
if (friend.m_strEmail.equals(D.username)){
mArrayAdaptor.add(friend.m_deviceInfo.m_strName);
Log.d("java", friend.m_strEmail + " -- " + friend.m_deviceInfo.m_strName);
}
else
mArrayAdaptor.add(friend.m_strEmail);
}
mAutoComepleteTextView.setAdapter(null); //this work
mAutoComepleteTextView.setAdapter(mArrayAdaptor); //for me
}
});
}

关于java - Android - AutoCompleteTextView、showDropDown() 并不总是有效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15132849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com