How to read data from ParseObject in Android - android

I'm new to Parse.com and Android and I'm struggling to read data from a Parse Object. I read the documentation and downloaded the tutorials but it is not clear. I have a ParseObject named Issue and all I need is to read data from this object and display it to the user. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_issue);
Parse.enableLocalDatastore(this);
Parse.initialize(this, "QKpM7ar7aWwrbEeTcrSGJ5bDnLMCUtc1kCr26Enl", "MCBdIQ6Y0dTsIoahzJ44UfR1zHZPJMQPwiETwj47");
ParseQuery<ParseObject> query = ParseQuery.getQuery("Issue");
query.whereEqualTo("objectId", "2aRwvd1Rnv");
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> issueList, com.parse.ParseException e) {
if (e == null) {
Log.d("Issue", "Retrieved " + issueList.size() + " issue");
Toast.makeText(getApplicationContext(), "List" + issueList, Toast.LENGTH_LONG).show();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
}
I don't understand how I can read data from this ParseObject. The returned result issueList is this: List[com.parse.ParseObject#4b079f88]
And if I use issue.getString("objectId") I get an error. Please help.

Thanks to Mr. Shubendara I was able to fix this:
if (issueList!=null){
if (issueList.size()>0){
for( int i = 0; i<issueList.size(); i++){
String o = issueList.get(i).getString("Title");
Toast.makeText(getApplicationContext(), "List" + o, Toast.LENGTH_LONG).show();
}
}

Related

Not able to fetch data from parse.com

I am using the Parse SDK for Android. I am not able to retrieve the data I stored in the cloud. I stored it manually using the Parse dashboard by creating a class called "Average" and creating a few rows. One of the columns is "squarefeet".
I want to retrieve the object whose "squarefeet" equals the value stored in the variable area.
What am I doing wrong? The compiler doesn't execute the done(params..) method. Below is the code.
ParseQuery<ParseObject> query = ParseQuery.getQuery("Average");
query.whereEqualTo("squarefeet", area);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objList, ParseException e) {
if (e == null) {
if (objList.size() > 0) {
for (int i = 0; i < objList.size(); i++) {
ParseObject p = objList.get(i);
averageConsumption = p
.getNumber("average_consumption");
efficientConsumption = p
.getNumber("efficient_consumption");
}
}
} else {
//something went wrong!
}
}
});
Use this
int area=120;
ParseQuery<ParseObject> query = ParseQuery.getQuery("Average");
query.whereEqualTo("squarefeet", area);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + scoreList.size() + " scores");
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

Android Parse Query

I'm using the following iOS Parse query:
let predicate = NSPredicate(format: "(fromUserid = %# OR fromUserid = %#) AND (toUserid = %# OR toUserid = %#) AND seen = %#", profilePhotoID, friendProfilePhotoID, profilePhotoID, friendProfilePhotoID, false)
var query = PFQuery(className: "Message", predicate: predicate)
How can I do the same query using the Parse Android SDK? I don't see an option to do predicates like that.
You can query data from parse in android SDK is like given example...
ParseQuery<ParseObject> query = ParseQuery.getQuery("Message");
String[] ids = {profilePhotoID, friendProfilePhotoID};
query.whereContainedIn("fromUserid", Arrays.asList(ids));
query.whereContainedIn("toUserid", Arrays.asList(ids));
query.whereEqualTo("seen", false);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("Message", "Retrieved " + scoreList.size() + " Message");
} else {
Log.d("Message", "Error: " + e.getMessage());
}
}
});
For more details please refer Parse Query Document
let me know if anything needed..

Get a child of a parent using pointer

I am trying to get a child when I have his parent.
I created a child (comment) , a parent (post) and a pointer from the child to the parent-
// Create the post
ParseObject myPost = new ParseObject("Post");
myPost.put("title", "I'm Hungry");
myPost.put("content", "Where should we go for lunch?");
// Create the comment
ParseObject myComment = new ParseObject("Comment");
myComment.put("content", "Let's do Sushirrito.");
// Add a relation between the Post and Comment
myComment.put("parent", myPost);
// This will save both myPost and myComment
myComment.saveInBackground();
My query:
String t="";
ParseObject c;
ParseQuery<ParseObject> query =
ParseQuery.getQuery("Comment");
query.whereEqualTo("parent",myPost );
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list,com.parse.ParseException e) {
if (e == null) {
c= list.get(0);
t= c.getString("content");
Toast.makeText(getApplicationContext(), t , Toast.LENGTH_LONG).show();
} else {
Log.d("NY", "Model.getStudentById Error: " + e.getMessage());
}
}
});
but I did not get the comment.
I tried also to put query.include("parent"); but it did not work.
what should I do??
thanks
The only thing I can figure out is that you might be trying to retrieve the myPost object from another method before it's actually been saved to Parse.
When I copied your code into a test project, I couldn't get it to work. Here's an updated version that does work.
Note: I'm chaining the operations so that I'm sure (well, as sure as I can be... you still need good error/exception handling) that the previous operation completed before performing the next.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Parse.initialize(this, "<value>", "<value>");
createPost();
}
private void createPost() {
ParseObject myPost = new ParseObject("Post");
myPost.put("title", "I'm Hungry");
myPost.put("content", "Where should we go for lunch?");
mPost = myPost;
ParseObject myComment = new ParseObject("Comment");
myComment.put("content", "Let's do Sushirrito.");
myComment.put("parent", myPost);
myComment.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("Comment");
query.whereEqualTo("parent", mPost);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, com.parse.ParseException e) {
if (e == null) {
ParseObject c = list.get(0);
String t = c.getString("content");
Toast.makeText(getApplicationContext(), t, Toast.LENGTH_LONG).show();
} else {
Log.d("NY", "Model.getStudentById Error: " + e.getMessage());
}
}
});
}
});
}
The code above shows my "Hello World" Activity and then posts a Toast that says "Let's do Sushirrito."

Parse android application query: saving retrieved data

I use parse.com to get an Integer, which saves how many gold a user has.
I tried this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("gold");
query.whereEqualTo("username", username);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
//how can I save the gold as an integer?
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});
That depends key you used to store the gold in the ParseObject. Once you have that, you can just replace your comment with:
int score = scoreList.get(0).getInt(<keyForGold>);

How can I retrieve all the groups name from server on parse.com in android?

I am facing a big problem in parse api. I am able to create new group on server as well able to add user to the group also. I created many group on server like Friends, Sports, College etc. But how can I retrieve the names of all group from server?
Using the below code I can retrieve all the user in the group.
public void getUserList() {
String groupName="Friends"
final ParseUser currentUser = ParseUser.getCurrentUser();
if (currentUser != null) {
ParseRelation relation = currentUser.getRelation(groupName);
ParseQuery query = relation.getQuery();
try {
Log.e("Size ", "Size " + query.count());
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// query.whereEqualTo("username", null);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException arg1) {
// TODO Auto-generated method stub
try {
for (int i = 0; i < objects.size(); i++) {
ParseObject r = objects.get(i);
String name = r.getString("username").toString();
Log.e("user name ", "===>" + name);
}
} catch (Exception e) {
Toast.makeText(getApplicationContext(),
"Error : " + e.toString(), 1).show();
}
}
});
}
}
In above screen-shot I need all the names like Friends, MYGroup and xyz using one query.
Please help me.
To get all groups, you need to query the Group class (or whatever you have called it):
ParseQuery<ParseObject> query = ParseQuery.getQuery("Group");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> groupList, ParseException e) {
if (e == null) {
for (ParseObject group : groupList) {
Log.d("group", "Group name: " + group.get("name");
}
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
});

Categories

Resources