Android Parse Query - android

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..

Related

How to search data in Back4app

I'm creating an application on android, and I'm using back4app as my back end. How to search data from back4app using Android Search View?
Thanks in Advance...
at the moment I believe that the structure below can help you, it's just a example :)
ParseQuery<ParseObject> query = ParseQuery.getQuery("GameScore");
query.whereEqualTo("playerName", "Dan Stemkoski");
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());
}
}
});

Add to another user array Parse Android

In Parse I have another column in the User table known as 'Friends' which is an array. From my current user I am trying to add to another user's friends array.
I have tried:
//buddy = a string of the other person's username
//to add to CURRENT user's array (this is working)
UserList.user.addUnique("Friends", buddy);
UserList.user.saveEventually();
//this is to add to another user's array (NOT WORKING)
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", buddy);
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
// The query was successful.
for (ParseObject friend : objects) {
friend.addUnique("Friends",UserList.user.getUsername());
friend.saveEventually();
}
} else {
// Something went wrong.
Log.d("Username", "Error: " + e.getMessage());
}
}
});
Write a method in a java that passes data to the cloud, and calls the function on the cloud by its name:
// the passes to the cloud is stored on HashMap
HashMap<String, Object> params = new HashMap<String, Object>();
// user's name to add the buddy to it
params.put("username", username);
// buddy's name to pass to the cloud
params.put("buddy", buddy);
ParseCloud.callFunctionInBackground("addBuddyToFriendsList", params, new FunctionCallback<String>(){
public void done(String result, ParseException e){
// e==null means no errors in running the function
if(e==null){
System.out.println(result);
}else{
System.out.println(e.getMessage());
}
}
});
Ok, and now let's write the function to be used in the cloud (assuming you know how to deploy functions on the cloud):
[EDITED]
Parse.Cloud.define("addBuddyToFriendsList", function (request, response){
Parse.Cloud.useMasterKey();
var query = new Parse.Query(Parse.User);
query.equalTo("username", request.params.username);
query.find({
success: function(user) {
// query.find returns an array.....
user[0].addUnique("Friends", request.params.buddy);
user[0].save(null, {
success: function(success){
response.success();
},
error: function (error){
response.error(error);
}
});
},
error: function(object, error) {}
});
});
Try this:
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("username", buddy);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
// The query was successful.
for (ParseObject friend : objects) {
friend.addUnique("Friends",UserList.user.getUsername());
friend.saveEventually();
}
} else {
// Something went wrong.
Log.d("Username", "Error: " + e.getMessage());
}
}
});
I tried to call that class in different way then you did, I referred to it like a normal class. Usually this way works for me.
The thing is with "ParseUser" type is limited, for security reasons.

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

How to read data from ParseObject in 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();
}
}

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