Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AutoCompleteTextView not working after fragment transition

I have searched a lot and tried many workaround - but none seems to work.

In my fragment, I have AutoCompleteTextView with standard ArrayAdapter, which is dynamically filled in onActivityCreated() function (as below).

All works fine when the fragment is added first time. However after I replace this fragment (with autocomplete) with another fragment - and come back using 'back' button - I get the problem that the 'auto complete' stops behaving like 'auto complete' - so if I now type in it, but I don't get the 'suggestion dropdown' anymore.

One thing to mention is that I am not using device softInput for typing - since I am only required to take mobile number as input - I have own custom keys displayed on the screen. But I dont think it should create any issue.

Attached are 2 screenshots - 1) before replace fragment when auto-complete working fine 2) after replace fragment and coming back, when auto-complete stops showing suggestions (note I typed '981' again here).

before fragment replace

after fragment replace - 981 is typed again

Any help is welcome !!

// '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());
    }
}
like image 328
user6119169 Avatar asked Nov 08 '22 17:11

user6119169


1 Answers

I found the next workaround:

mAdapter.filter.filter("")

Just call this before your addAll statement when you update it.

Hope it helps! This one seriously bugged me for some time.

like image 118
emirua Avatar answered Nov 14 '22 23:11

emirua