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();
}
}
});