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

时间:2013-02-28 10:14:19

标签: java android view

当我每次都尝试动​​态更新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
             }
    });
}

感谢任何帮助,谢谢。

1 个答案:

答案 0 :(得分:1)

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

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