如何获取 Parse 用户名列表<ParseUser>?
How to get a List<ParseUser> of Parse usernames?
我的 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());
}
}
});
问题是这个数组包含所有列,而我只想获取 "username" 字段。我怎样才能只得到 "username" 字段?
另一个问题是,因为在 User table 中有一个名为 "emailVerified" 的布尔字段,而 aAdapter 是一个 ArrayAdapter<String
它里面不能有布尔值,所以应用程序崩溃了。那么,我怎样才能只获得 "username" 字段?
Logcat 崩溃:
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.done(GameActivity.java:189)
at com.intap.tof.GameActivity.done(GameActivity.java:184)
at com.parse.ParseTaskUtils.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 因@MarcoBatista 的回答而崩溃:
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.done(GameActivity.java:192)
at com.intap.tof.GameActivity.done(GameActivity.java:184)
at com.parse.ParseTaskUtils.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)
错误行:
GameActivity.java:192 - friendChooser.setAdapter(aAdapter);
GameActivity.java:184 - public void done(List<ParseUser> userList, ParseException e) {
给你:
aAdapter
是一个 ArrayAdapter<String>
。
friendChooser
是一个 MultiAutoCompleteTextView
。
friends
是一个 List<String>
。
userList
是 List<ParseUser>
.
而不是这个:
friends.add(userList.toString());
试试这个:
friends.add(userList.get(i).getUsername());
您必须从列表中获取对象,然后从中获取用户名 (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());
}
}
});
我的 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());
}
}
});
问题是这个数组包含所有列,而我只想获取 "username" 字段。我怎样才能只得到 "username" 字段?
另一个问题是,因为在 User table 中有一个名为 "emailVerified" 的布尔字段,而 aAdapter 是一个 ArrayAdapter<String
它里面不能有布尔值,所以应用程序崩溃了。那么,我怎样才能只获得 "username" 字段?
Logcat 崩溃:
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.done(GameActivity.java:189)
at com.intap.tof.GameActivity.done(GameActivity.java:184)
at com.parse.ParseTaskUtils.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 因@MarcoBatista 的回答而崩溃:
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.done(GameActivity.java:192)
at com.intap.tof.GameActivity.done(GameActivity.java:184)
at com.parse.ParseTaskUtils.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)
错误行:
GameActivity.java:192 - friendChooser.setAdapter(aAdapter);
GameActivity.java:184 - public void done(List<ParseUser> userList, ParseException e) {
给你:
aAdapter
是一个 ArrayAdapter<String>
。
friendChooser
是一个 MultiAutoCompleteTextView
。
friends
是一个 List<String>
。
userList
是 List<ParseUser>
.
而不是这个:
friends.add(userList.toString());
试试这个:
friends.add(userList.get(i).getUsername());
您必须从列表中获取对象,然后从中获取用户名 (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());
}
}
});