0

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>.

2 Answers2

1

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());
        }
    }
});
Marco Batista
  • 1,410
  • 1
  • 20
  • 38
  • It still crashes. I will add the logcat to my question. –  Aug 27 '15 at 19:25
  • you have to initialize the friends array, I'll edit the answer with the initialization – Marco Batista Aug 27 '15 at 19:27
  • It still crashes, but now it shows a different error message: `Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference`. What can I do to solve this? –  Aug 27 '15 at 20:12
  • Update your question with the full logcat and the line that is causing the crash – Marco Batista Aug 27 '15 at 20:38
  • @MarcoBatista I'll do it now – Ido Naveh Aug 28 '15 at 07:21
  • @MarcoBatista I've added the logcat, and the error lines. Please look. –  Aug 28 '15 at 07:27
  • is the `context` variable initialized? Try replacing it with `GameActivity.this` – Marco Batista Aug 28 '15 at 08:24
  • @MarcoBatista I've initialized the context, and it doesn't crash down, but it doesn't show any results... –  Aug 28 '15 at 09:22
  • That seems a different problem, accept this answer and open a new question with that problem and add the relevant code. Post the link to the new question here and I'll try to help you :) – Marco Batista Aug 28 '15 at 10:34
  • @MarcoBatista Here is the link to the new question: http://stackoverflow.com/questions/32270983/multiautocompletetextview-doesnt-show-results Thanks for the help! –  Aug 28 '15 at 12:26
0

Instead of this:

 friends.add(userList.toString());

Try this:

 friends.add(userList.get(i).getUsername());
Zsolt Mester
  • 1,053
  • 9
  • 14