1

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

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

4

1 回答 1

3

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

    @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
        }
    });
}
于 2013-02-28T15:28:42.587 回答