通过用户文本输入限制ParseQuery

时间:2015-12-16 02:32:58

标签: java android parse-platform autocompletetextview

我正在尝试为我的ParseQuery写一个限制,其中查询只返回包含我的用户输入的前两个字符到AutoCompleteTextView的用户名。我可以使用substring来查找输入AutoCompleteTextView的字符串的前两个字符,然后根据它运行查询吗?

    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
        String input = textView.toString();
        String recommendation = input.substring(0,2);
        userQuery.whereStartsWith(ParseConstants.KEY_USERNAME, recommendation);
        userQuery.findInBackground(new FindCallback<ParseUser>() {
        ...

我意识到这并不是你真的如何做到这一点,而且我还没有设置处理程序来根据文本的变化来生成新的查询,但我试图在婴儿步骤中解决这个问题。 / p>

1 个答案:

答案 0 :(得分:0)

这就是我在Activity的onCreate()方法中所拥有的:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // Get the view from listview_main.xml
    setContentView(R.layout.activity_search);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);

    final AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
    textView.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before,
                                  int count) {
            if (count % 2 == 1) {
                new Handler().postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        textView.showDropDown();
                        UserSuggestionQuery(textView);
                    }
                }, 1000);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            UserSuggestionQuery(textView);
        }

        public void afterTextChanged(Editable s) {
            UserSuggestionQuery(textView);
        }
    });
    textView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

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

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

        }
    });
    textView.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event){
            UserSuggestionQuery(textView);
            return false;
        }
    });

    SubmitSearch();

    // Execute RemoteDataTask AsyncTask
    new RemoteDataTask().execute();

}

以上代码中出现以下功能。这是输入到AutoCompleteTextView的用户的实际查询:

    private void UserSuggestionQuery(AutoCompleteTextView textView) {
        ParseQuery<ParseUser> userQuery = ParseUser.getQuery();
        String input = textView.getText().toString();
        System.out.println(input);
        String recommendation = input.substring(0);
        userQuery.whereStartsWith(ParseConstants.KEY_USERNAME, recommendation);
        userQuery.findInBackground(new FindCallback<ParseUser>() {
            public void done(final 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).toLowerCase();
                    }
                    System.out.println(Arrays.toString(strings));
                    final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_selectable_list_item, strings);
                    AutoCompleteTextView textView = (AutoCompleteTextView) findViewById(R.id.searchUserTextField);
                    textView.showDropDown();
                    textView.setThreshold(1);
                    textView.setAdapter(adapter);

                    // Query based on text change
                    adapter.setNotifyOnChange(true);

                }
            }
        });
    }