问题
I have made an application where users can log in and post text to my Parse database. All posts are held in a text column inside a Posts class, another column in this class is user which is a pointer to my User class which has a column called username, it is the username string that I would like to retrieve.
I've managed to query the database and retrieve all of the data within the text column, put it all into a string array and then into a listview in my app.
what I would like to achieve next is to retrieve all of the posts in the text column and the name of the user that posted each one by looking at the related username column within Posts.
Can I do this all in one query or should I create another query to do this?
Parse classes:
User
| objectID (String) | username (String) | password (String) |
Posts
| objectID (String) | text (String) | user (Pointer<_User>) |
the user would start app, register a username and password which adds a row to the User class containing their username and password. The user would then make a post which would add a row to the Posts class containing text and a pointer to the user that made the post within the user column.
query and ListView code:
//create new ArrayList and ArrayAdapter
final ArrayList<String> postList = new ArrayList<String>();
final ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(this, R.layout.listview_row, postList);
//create new ParseQuery selecting all data within 'text' in descending order
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
//query
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> text, ParseException e) {
if (e == null) {
//query successful
int listSize = text.size();
//loop through each post in 'text'
for(int i = 0; i < listSize; i++){
//add each post to textArray
textArray[i] = text.get(i).getString("text");
}
//add all data in textArray to ListView
postList.addAll(Arrays.asList(textArray));
lvText.setAdapter(listAdapter);
}
else {
//query error
Toast.makeText(MainActivity.this, "query error: " + e, Toast.LENGTH_LONG).show();
}
}
});
回答1:
It's rather simple, you can use a .include on an existing query to "Include the [class you want to include] data with each [Class you're querying]" and you can call the queried object I believe as follow:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
query.include([field containing user reference]);
ParseObject parseUser = text.get(i).getParseObject(([field containing user reference]);
I hope this helps!
UPDATE
"I forgot to mention that my 'user' column within the Posts class is a pointer to the objectID of the user within my User class. Within the User class is a username column, this is the information that I would like to feed back to my application.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
query.include("user");
String parseUserName = text.get(i).getParseObject(("user").getString("userName");
It might be that this does not work, with an error saying you have to refresh the object to get the data. But first try this and see if it works it magic, I know some other tricks which are:
ParseQuery<ParseObject> query = ParseQuery.getQuery("Posts");
query.selectKeys(Arrays.asList("text"));
query.addDescendingOrder("text");
query.include("user");
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> text, ParseException e) {
if (e == null) {
ParseQuery<ParseUser> query = ParseQuery.getQuery("_User");
query.whereEqualTo("objectId", text.get(0).getParseObject(""));
query.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> users, ParseException e) {
if (e == null) {
String parseUserName = users.get(0).getString("userName");
}
});
int listSize = text.size();
//loop through each post in 'text'
for(int i = 0; i < listSize; i++){
来源:https://stackoverflow.com/questions/29095887/android-parse-query-to-get-all-posts-and-the-users-to-go-with-the-posts