user6119169
user6119169

Reputation: 131

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

Upvotes: 7

Views: 1134

Answers (2)

Adham Gamal
Adham Gamal

Reputation: 795

It happened to me but in different style of code, What I did is setting the adapter every single time and not using notifydatachanges, It is not best practice or even near, but in my case changes wasn't even count.

autoCompleteTextView.setAdapter(adapter); 

Upvotes: 0

emirua
emirua

Reputation: 508

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.

Upvotes: 1

Related Questions