简体   繁体   中英

AutoCompleteTextview doesn't show dropdown suggestion for first character entry

I have an AutoCompleteTextView searchFriends and a String[] usernames= {"Lex","Lincoln","Luther"} that I get from a database.

Problem: The first time I type in the letter "L" in the AutoCompleteTextView , no suggestions are shown. If I press backspace to remove the "L" and then retype "L" again, then "Lex", "Lincoln" and "Luther" are suggested. I want suggestions to be shown the first time something is entered.

public class SearchFriendsActivity extends AppCompatActivity {

Firebase userbase;

AutoCompleteTextView searchFriends;

ArrayAdapter adapter;

char firstChar;
String url, listOfusers, inputUsername, userss;
String[] usersArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_friends);

    searchFriends= (AutoCompleteTextView)findViewById(R.id.searchFriends);

    searchFriends.addTextChangedListener(new TextWatcher() {

        private Timer timer = new Timer();
        private final long DELAY = 1000; // milliseconds

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {                   
                }


        @Override
        public void afterTextChanged(Editable s) {
            timer.cancel();
            timer = new Timer();
            timer.schedule(
                    new TimerTask() {
                        @Override
                        public void run() {
                            ((Activity) context).runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    if (s.length() == 0) {
                                        firstChar = '.';
                                    } else {
                                        if (firstChar == s.charAt(0)) {
                                            aVoid();
                                        } else {
                                            firstChar = s.charAt(0);
                                            url = @string/databaseString + firstChar.toString() + "/";
                                            userbase = new Firebase(url);
                                            createArray();
                                        }
                                    }
                                }
                            });
                        }
                    },
                    DELAY
            );

        }
    });

}

public void createArray() {
    userbase.addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listOfusers = dataSnapshot.getValue().toString();
            userss = listOfusers.replaceAll("=.*?\\}", "").replace("{", "").replace("}", "").replace(", ", ",");
            usersArray = userss.split(",");

            aVoid();
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });
}

public void aVoid() {
    adapter = new ArrayAdapter(this, R.layout.suggestion_dropdown_item, usersArray);
    searchFriends.setThreshold(1);
    searchFriends.setAdapter(adapter);
}
}

Once you have the entire list in the adapter, it's as easy as this:

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        Filterable adapter = (Filterable) searchFriends.getAdapter();
        adapter.getFilter().filter(s);
    }

Try putting the code inside onTextChanged into afterTextChange because in onTextChanged , the text inside the AutoCompleteTextView is not yet set to it evertime you input a character. So on your first input (ex. "L"), the value of AutoCompleteTextView.getText().toString; is still null , that is why you have your problems.

onTextChanged is commonly used to capture the Key pressed to perform checking or validation before appending it to previous value of the AutoCompleteEditText .

aftetTextChange on the otherhand is fired (as the name implies) after the previous value of the AutoCompleteEditText is changed. So your code is best put inside afterTextChanged rather than in onTextChanged .

I worked around this problem by replacing the text with the same text in the edittext a moment after the adapter is created:

public void replaceAllText() {

    Timer timer = new Timer();
    final long DELAY = 300; // milliseconds

    timer.cancel();
    timer = new Timer();
    timer.schedule(
            new TimerTask() {
                @Override
                public void run() {
                    ((Activity) context).runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            String replace = searchFriends.getText().toString();
                            searchFriends.setText(replace);
                            searchFriends.setSelection(searchFriends.getText().length());
                        }
                    });
                }
            },
            DELAY
    );
}

将此行添加到AutoCompleteTextView

android:completionThreshold="1"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM