简体   繁体   English

具有Parse.com的AutoCompleteTextView(Android)

[英]AutoCompleteTextView with Parse.com (Android)

I'm new to Android in general, so I looked up the documentation for AutoCompleteTextView . 一般而言,我是Android新手,所以我查阅了AutoCompleteTextView的文档。

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
        android.R.layout.simple_dropdown_item_1line, USERS);
AutoCompleteTextView textView = (AutoCompleteTextView)
        findViewById(R.id.searchUserTextField);
textView.setAdapter(adapter);

I see that it's up and running with very little code. 我看到它只需很少的代码即可启动并运行。

private static final String[] COUNTRIES = new String[] {
        "user1", "user1", "user3", "user4", "user5"
};

I realize this is a stretch, but what would the next steps be with respect to implementing an autocomplete function based on my ParseUser table, especially as the text in the AutoCompleteTextView is changed by one character at a time?. 我意识到这是一个麻烦,但是基于ParseUser表实现自动完成功能的下一步是什么,尤其是当AutoCompleteTextView的文本一次更改一个字符时? Obviously, I wouldn't populate the USERS array with a Parse query displaying all of my users on each attempted search. 显然,我不会在USERS数组中填充一个Parse查询,该查询显示每次尝试搜索时我的所有用户。 How would I logically arrange such a thing? 我将如何逻辑地安排这样的事情?

To begin with, I'd probably include a TextChangedListener : 首先,我可能会包含一个TextChangedListener

adapter.setNotifyOnChange(true);
    textView.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before,
                int count) {
            if (count % 3 == 1) {
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        textview.setDropDownHeight(LayoutParams.WRAP_CONTENT);
                        adapter.clear();
                        // Run my background task here


                    }
                }, 1000);
            }
        }

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

        }

        public void afterTextChanged(Editable s) {

        }
    });
    textView.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterview, View v,
                int position, long arg3) {
            // TODO Auto-generated method stub
            searchUserTextField.setText(adapterview.getItemAtPosition(position)
                    .toString());
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

But what would I provide as the background task? 但是我将提供什么作为后台任务? What am I querying so that I'm not searching through, say, 1000 users all at once, which is how many users I have in my app currently? 我要查询什么,这样我就不会一次搜索全部1000个用户,这是我目前在我的应用程序中有多少个用户?

This seems to work. 这似乎有效。 It's static in the sense that it always prints out the first 100 users in your User class. 从某种意义上讲,它始终是静态的,它始终会打印出User类中的前100个用户。 I'm sure you could add a constraint that matches the first or second letters of the AutoCompleteTextView to the usernames. 我确定您可以添加一个约束,以使AutoCompleteTextView的第一个或第二个字母与用户名匹配。

ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
userQuery.findInBackground(new FindCallback<ParseUser>() {
    public void done(List<ParseUser> parseUsers, ParseException e) {
        if (e == null) {
            Log.d("users", "Retrieved " + parseUsers.size());
            ParseUser[] data = parseUsers.toArray(new ParseUser[parseUsers.size()]);
            String[] strings = new String[data.length];
            for (int i = 0; i < data.length; i++) {
                strings[i] = data[i].getString(ParseConstants.KEY_USERNAME);
            }
            // Test to see if it was correctly printing out the array I wanted.
            // System.out.println(Arrays.toString(strings));
            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_dropdown_item_1line, strings);
            AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
            if(parseUsers.size() < 40) textView.setThreshold(1);
            else textView.setThreshold(2);
            textView.setAdapter(adapter);
        } else {
            Log.d("users", "Error: " + e.getMessage());
        }
    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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