androidlistparse-platformandroid-arrayadaptermultiautocompletetextview

How to get a List<ParseUser> of Parse usernames?


I have this code on my activity:

ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.findInBackground(new FindCallback<ParseUser>() {
        public void done(List<ParseUser> userList, ParseException e) {
            if (e == null) {
                Log.d("score", "Retrieved " + userList.size() + " scores");
                for (int i = 0; i < userList.size(); i++) {
                    friends.add(userList.toString());
                }
                aAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, friends);
                friendChooser.setAdapter(aAdapter);
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });

The problem is that this array contains all of columns, when I want to get only the "username" field. How can I get only the "username" field?
Another problem is that because that in the User table there is a boolean field called "emailVerified", and aAdapter is an ArrayAdapter<String it cannot have a boolean inside, so the app crashed. So again, how can I get only the "username" field?

Logcat of crash:

java.lang.NullPointerException: Attempt to invoke interface method 'boolean java.util.List.add(java.lang.Object)' on a null object reference
        at com.intap.tof.GameActivity$3.done(GameActivity.java:189)
        at com.intap.tof.GameActivity$3.done(GameActivity.java:184)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5832)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

Logcat on crash with @MarcoBatista's answer:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
        at com.intap.tof.GameActivity$3.done(GameActivity.java:192)
        at com.intap.tof.GameActivity$3.done(GameActivity.java:184)
        at com.parse.ParseTaskUtils$2$1.run(ParseTaskUtils.java:107)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)

The error lines:
GameActivity.java:192 - friendChooser.setAdapter(aAdapter);
GameActivity.java:184 - public void done(List<ParseUser> userList, ParseException e) {

For you:
aAdapter is an ArrayAdapter<String>.
friendChooser is a MultiAutoCompleteTextView.
friends is a List<String>.
userList is a List<ParseUser>.


Solution

  • You have to get the object from the list and then get the Username from it (docs):

    ParseQuery<ParseUser> query = ParseUser.getQuery();
    query.findInBackground(new FindCallback<ParseUser>(){
        public void done(List<ParseUser> userList, ParseException e) {
            if (e == null) {
                Log.d("score", "Retrieved " + userList.size() + " scores");
                friends = new ArrayList<>();//<-- Initialize the String array
                for (int i = 0; i < userList.size(); i++) {
                    friends.add(userList.get(i).getUsername());
                }
                aAdapter = new ArrayAdapter<String>(context, android.R.layout.simple_dropdown_item_1line, friends);
                friendChooser.setAdapter(aAdapter);
            } else {
                Log.d("score", "Error: " + e.getMessage());
            }
        }
    });