How to retrieve object elements from a list - android

I am trying to retrieve some data from a couple of parse tables using parse cloud code.
ParseCloud.callFunctionInBackground("getData", parameters, new FunctionCallback<List>() {
#Override
public void done(List results, ParseException e) {
if (e == null) {
for (ParseObject country : results) {
}
}
}
});
The List results returns all the data from all the tables included in the cloud code. But I don't know how to access them.
Here is the data that the arrayList retrieves:
[{startup=com.parse.ParseObject#1480b86a,
resolvedRelations=
{advisors=[com.parse.ParseObject#3efb015b, com.parse.ParseObject#32ab7df8, com.parse.ParseObject#393662d1],
activeSyndicate= {investors=[com.parse.ParseObject#2ce9f136, com.parse.ParseObject#2cde5837, com.parse.ParseObject#b1681a4, com.parse.ParseObject#3ced5b0d, com.parse.ParseObject#3bdbeac2]},
images=[com.parse.ParseObject#123640d3, com.parse.ParseObject#3f0c1410],
founders=[com.parse.ParseObject#1aa19b09, com.parse.ParseObject#601f10e, com.parse.ParseObject#251a172f],
tags=[com.parse.ParseObject#1c86613c, com.parse.ParseObject#73fdec5, com.parse.ParseObject#1d70101a, com.parse.ParseObject#3bbaf74b, com.parse.ParseObject#34f65528, com.parse.ParseObject#1fc4a241, com.parse.ParseObject#2f6f13e6]}}
The ArrayList returns objects, but I need ParseObject. Help please.

Related

Converting Parse Relation to list in adapter class

I would like a populate a TextView in one of my fragments with a list of users who are attending a specific trip. On the Parse server, Trip contains a Parse Relation of users attending each instance of a Trip. The following method is in my Trip model class to retrieve a String list of the users' names.
public List<String> getMemberNames(){
final List members = new ArrayList<String>();
try{
ParseRelation<ParseUser> relation = getRelation(KEY_USER);
ParseQuery query = relation.getQuery();
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> parseUsers, ParseException e) {
for (ParseUser parseUser : parseUsers)
{
members.add(parseUser.getUsername());
}
Log.d("relation", members.toString());
}
});
}catch (Exception e){
e.printStackTrace();
}
return members;
}
In my adapter, I call this with the line:
viewHolder.tvMembers.setText(Arrays.toString(trip.getMemberNames().toArray()));
However, when I run the debugger, the list of members remains empty. Any advice on why this is? Is is not possible to query in the Trip model? I appreciate the help!

Android Parse - table of pointers to other tables, want to retrieve objects in the other tables

I feel like I'm pretty close on this one, just need the last bit.
I have the following tables:
_User (standard Parse table)
Category (object Id, name)
Exercises (object Id, name, description, thumbnail, image, etc)
and UserFavourites which is where I store the user's preferred exercises
(objectId, user->users table, exercise->exercises table, category->category table)
I have writing to Parse using pointers just fine:
//create new parse object
ParseObject favouriteExercise = new ParseObject("UserFavourites");
//create pointers to the Exercise table and Category table
ParseObject exercise = ParseObject.createWithoutData("Exercises", mExerciseId);
ParseObject category = ParseObject.createWithoutData("Category", mCategoryId);
//put those pointers into the Userfavourites table and save
favouriteExercise.put("user",ParseUser.getCurrentUser());
favouriteExercise.put("exercise",exercise);
favouriteExercise.put("category",category);
//save
favouriteExercise.saveInBackground();
Now I'm trying to retrieve all the exercises a user has favourited and put them in to a listview by searching the table for any objects that match the user's pointer to the user's table:
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> exercises, ParseException e) {
if (exercises != null) {
Toast.makeText(getApplicationContext(), "Favourites found, can't list yet", Toast.LENGTH_SHORT).show();
mAdapter.clear();
//add all the exercises to the list
mAdapter.addAll(exercises);
//sort the list alphabetically
mAdapter.sort(new Comparator<Exercises>() {
#Override
public int compare(Exercises exercises, Exercises t1) {
return exercises.getName().compareTo(t1.getName());
}
});
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
Where I'm stuck is when I run this I can see my query is working -> I am retrieving the 4 rows in UserFavourites that I favourited out of the table of 8, so it is filtering correctly, but the objects I'm getting aren't pointing to the exercises I want. They are just empty pointers.
Thanks.
Yes it will return only reference (Pointer). If you want actual object data call fetchInBackground
myObject.fetchInBackground(new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
// Success!
} else {
// Failure!
}
}
});
I figured it out based on the logic kishore jethava gave.
I queried the favorites table, then with the results I wanted (which pointed to another table) I cycled through each result and got the object it pointed to and added it to my ArrayList.
public void getFavourites() {
//set progress bar
setProgressBarIndeterminateVisibility(true);
ParseQuery<Exercises> query = ParseQuery.getQuery("UserFavourites");
final ParseObject user = ParseObject.createWithoutData(ParseUser.class, ParseUser.getCurrentUser().getObjectId());
query.whereEqualTo("user", user);
query.include("exercise");
//call to parse.com to start the query
query.findInBackground(new FindCallback<Exercises>() {
#Override
public void done(List<Exercises> objects, ParseException e) {
if (objects.size() != 0) {
for(ParseObject object : objects)
{
//for each pointer found, retrieve the object it points to
obj = object.getParseObject("exercise");
mAdapter.add((Exercises) obj);
}
});
}
} else {
mNoFavourites.setVisibility(View.VISIBLE);
}
//stop progress bar
setProgressBarIndeterminateVisibility(false);
}
});
}

A different way to retrieve an Object in Parse with Android Studios

Currently I am making an Android application with Android Studios and I am using Parse to handle the data. I want to be able send (put) data such as town names and info about that town (town_names would be a column and info would be another, there for each town_names would have its own info). I can do that with ease and it uploads to the Parse database. However, the part I can not seem to figure out is how to retrieve the data points I desire. I have code to retrieve data based off of an objectId shown below however that is not exactly what I want.
ParseQuery<ParseObject> query = ParseQuery.getQuery("userMessage");
query.getInBackground("KgsLojXPcq", new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
retrievedText = object.getString("info");
} else {
retrievedText = "No information.";
}
}
});
"userMessage" is the ParseObject I have created and "info" is the column I want to get text from. Instead of searching for an objectId ("KgsLojXPcq") is there a way to search for town_names? So then I could for example search the database for "New York" and "retrievedText" would be set to the info for New York.
ParseQuery
.getQuery("userMessage")
.whereEqualTo("town_names", "New York")
.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null) {
retrievedText = object.getString("info");
} else {
retrievedText = "No information.";
}
}
});

Getting data from Parse query

I am having some trouble getting some data from my Parse table/object with a query. I am trying to simply make a query which looks for the current Parse User's objectID in the "sender" column. When that result is returned, I want to extract the receiver's objectID from the "receiver" column associated with the user that I searched for. I keep getting 0 results, even though I know the data is there. Here is my code:
private List<String> potentialRelationQuery() {
mPotentialRelations = new ArrayList<>();
String currentUserId = mCurrentUser.getObjectId();
ParseQuery<ParseObject> query3 = ParseQuery.getQuery("PotentialRelation");
query3.whereEqualTo("sender", currentUserId);
query3.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseObjects, ParseException e) {
if (e == null) {
if (parseObjects.size() > 0) {
for (int i = 0; i < parseObjects.size(); i++) {
ParseUser receiver = (ParseUser) parseObjects.get(i).get("receiver");
String receiverId = receiver.getObjectId();
mPotentialRelations.add(receiverId);
}
}
} else {
Log.d("MyApp", "No matching objects returned from request");
}
}
});
return mPotentialRelations;
}
Since findInBackground is an asynchronous call to Parse isn't it possible that mPotentialRelations returns empty because the findInBackground query hasn't yet completed before the potentialRelationQuery method returns? I know I've had issues with this. Since you can't return data from an inner class (i.e. in the done method of FindCallback), writing this kind of query method has never really worked consistently for me.

Parse.com - How to obtain ParseUser from ParseObject

I am trying to retrieve 2 values from my Cloud Code function. A List of ParseUser and a List of ParseObject.
This is a snippet of the Cloud Code.
...
var finalResult = {"users":users,"groups":groups};
//users is an array of Parse.User
//groups is an array of Parse.Object
response.success(finalResult);
This is the call from the client.
ParseCloud.callFunctionInBackground("serviceUpdate", new HashMap<String, Object>(),new FunctionCallback<HashMap<String, List<ParseObject>>>() {
public void done(HashMap<String, List<ParseObject>> result, ParseException e) {
if (e == null) {
List<ParseObject> users = result.get("users");
List<ParseObject> groups = result.get("groups");
As you can see, users is now a ParseObject. How can I obtain the ParseUser? Any suggestion is appreciated.
Can't you use casting, as follows:
ParseCloud.callFunctionInBackground("serviceUpdate", new HashMap<String, Object>(),new FunctionCallback<HashMap<String, List<ParseObject>>>() {
public void done(HashMap<String, List<Object>> result, ParseException e) {
if (e == null) {
List<ParseUser> users = result.get("users");
List<ParseObject> groups = result.get("groups");
To obtain as results Parse User you have to query the "User" table with this specific query https://parse.com/docs/android/guide#users-querying.
So you can retrieve the unique id from your posted query (it is a string) and then use that as filter for the new query.

Categories

Resources