AutoCompleteTextview不显示第一个字符输入的下拉建议

时间:2016-07-14 15:36:39

标签: java android arrays autocompletetextview

我从数据库中获得AutoCompleteTextView searchFriendsString[] usernames= {"Lex","Lincoln","Luther"}

问题:我第一次在AutoCompleteTextView中输入字母“L”时,没有显示任何建议。如果我按退格键移除“L”然后再次重新键入“L”,则建议使用“Lex”,“Lincoln”和“Luther”。我希望在第一次输入内容时显示建议。

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

4 个答案:

答案 0 :(得分:1)

在适配器中包含整个列表后,它就像这样简单:

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

答案 1 :(得分:1)

尝试将onTextChanged内的代码放入afterTextChange,因为在onTextChanged中,AutoCompleteTextView内的文字尚未设置为它,此时您输入了一个字符。因此,在您的第一个输入(例如“L”)上,AutoCompleteTextView.getText().toString;的值仍为null,这就是您遇到问题的原因。

onTextChanged通常用于捕获按键以执行检查或验证,然后将其附加到AutoCompleteEditText的上一个值。

另一方面的

aftetTextChangeAutoCompleteEditText的前一个值发生更改后被触发(顾名思义)。因此,您的代码最好放在afterTextChanged内,而不是onTextChanged

答案 2 :(得分:0)

我在创建适配器后的一刻用edittext中的相同文本替换文本来解决这个问题:

SELECT cpu_usage
FROM cpus
WHERE cpu_id = 1
ORDER BY cpu_ts DESC
LIMIT 1

答案 3 :(得分:0)

将此行添加到AutoCompleteTextView

android:completionThreshold="1"