flyingsilverfin
flyingsilverfin

Reputation: 450

Android AutocompleteTextView fill in when one option left

I am trying to make an AutoCompleteTextView that automatically fills in the autocomplete when there is only 1 option left in the filtered list.

Here's what I've got so far:

import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.SpannableString;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.MultiAutoCompleteTextView;

public class InstantAutoComplete extends AutoCompleteTextView {

private final static String TAG = "InstantAutoComplete";
public InstantAutoComplete(Context context) {
    super(context);
}

public InstantAutoComplete(Context arg0, AttributeSet arg1) {
    super(arg0, arg1);
}

public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
    super(arg0, arg1, arg2);

}

@Override
protected void replaceText(CharSequence text) {
    Log.i(TAG, "REPLACING TEXT");
    super.replaceText(text);

}

@Override
public CharSequence convertSelectionToString(Object obj) {
    //This needs to return a Spanned object
    return new SpannableString(super.convertSelectionToString(obj));
}

@Override
public void onFilterComplete(int count) {
    Log.i(TAG, "Count: " + count);
    super.onFilterComplete(count);
    if(count == 1) {
        clearListSelection();
        Log.i(TAG, "Is showing: " + isPopupShowing());
        setListSelection(0);
        Log.i(TAG, "Selected: "+getListSelection());
        performCompletion();
    }
}
}

So far I've figured out that I can check if there's only 1 item left in the dropdown in the onFilterComplete. However Beyond that, I'm stuck. For some reason, setListSelection isn't doing anything. If it were, I could use that to set the selected item, and then performCompetion should run as as usual. Besides that, I'm not sure that setListSelection is relative to the filtered list or to the original, longer list; but that's the next problem to solve.

So, any help on why setListSelection is not working, or on how to solve this problem in general is appreciated!

Upvotes: 2

Views: 1044

Answers (1)

triad
triad

Reputation: 21537

setListSelection didn't work for me also.

I looked into the AutoCompleteTextView source code and found that a dropdown selection could be invoked by calling onCommitCompletion(new CompletionInfo(0, index, null));

Hopefully this helps someone looking for a solution.

Upvotes: 2

Related Questions