【问题标题】:AutoCompleteTextView not working after fragment transition片段转换后 AutoCompleteTextView 不起作用
【发布时间】:2016-03-27 00:12:10
【问题描述】:

我进行了很多搜索并尝试了很多解决方法 - 但似乎没有一个有效。

在我的片段中,我有带有标准 ArrayAdapter 的 AutoCompleteTextView,它在 onActivityCreated() 函数中动态填充(如下所示)。

第一次添加片段时一切正常。然而,在我用另一个片段替换这个片段(使用自动完成)之后 - 并使用“返回”按钮返回 - 我得到的问题是“自动完成”停止表现得像“自动完成” - 所以如果我现在输入它,但是我不再收到“建议下拉菜单”了。

要提一提的是,我没有使用设备 softInput 进行打字——因为我只需要输入手机号码——我在屏幕上显示了自己的自定义键。但我认为这不会造成任何问题。

附件是 2 个屏幕截图 - 1) 替换片段之前自动完成工作正常 2) 替换片段之后返回,当自动完成停止显示建议时(注意我在这里再次输入了“981”)。

before fragment replace

after fragment replace - 981 is typed again

欢迎任何帮助!!

// 'mCustMobileNums' is a singleton class which fetches strings stored in a DB table.    
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    Log.d(TAG, "In onActivityCreated");
    super.onActivityCreated(savedInstanceState);

    if (mCustMobileNums==null) {
        mCustMobileNums = CustomerMobileNums.getInstance(getActivity().getApplicationContext());
    }
    initInputCustMobile();
}

private void initInputCustMobile() {
    if(mAdapter==null) {
        Log.d(TAG, "Creating autocomplete adapter instance.");
        mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_dropdown_item_1line);
        mAdapter.addAll(mCustMobileNums.getCollection());
        mAdapter.setNotifyOnChange(false);
    }
    mInputCustMobile.setAdapter(mAdapter);
    Log.d(TAG, "Initialized autocomplete adaptor: " + mAdapter.getCount());
}

// This function is called when I have insert new entry in the data set
public void updateAutoCompleteAdaptor(String mobileNum) {
    Log.d(TAG,"In updateAutoCompleteAdaptor");
    // add in memory and db
    // will return TRUE if entry was not already available and added successfully
    if( mCustMobileNums.addCustMobileNum(mobileNum) ) {
        // recreate with sorted set
        mAdapter.clear();
        mAdapter.addAll(mCustMobileNums.getCollection());
        mAdapter.notifyDataSetChanged();
        Log.d(TAG,"Updated autocomplete adaptor: "+mAdapter.getCount());
    }
}

【问题讨论】:

  • 它是否在不同的设备上执行此操作?某些 Android 版本?

标签: android fragment autocompletetextview


【解决方案1】:

我找到了下一个解决方法:

mAdapter.filter.filter("")

更新时只需在 addAll 语句之前调用它即可。

希望对您有所帮助!这个问题严重困扰了我一段时间。

【讨论】:

    【解决方案2】:

    这发生在我身上,但代码风格不同,我所做的是每次都设置适配器而不使用notifydatachanges,这不是最佳实践,甚至不是接近的,但在我的情况下,变化甚至不算数.

    autoCompleteTextView.setAdapter(adapter); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-09-22
      • 2017-07-13
      • 1970-01-01
      • 2012-11-26
      • 1970-01-01
      • 1970-01-01
      • 2015-04-20
      • 1970-01-01
      相关资源
      最近更新 更多