Parse android application query: saving retrieved data - android

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

Related

I want to search a specific string form a particular column(contains Strings) in my parse table in android

I want to search a specific string "f2b5e2511827bc26a1b43d73fa76dc5a0a34daa8" from "manual_friendID" column in my parse table named as "Manual_FriendList"
Use ParseQuery to look for data. Initialzie it with the class name, then add custom conditions on the required data, in our case search for column 'manual_fiendID' to be equal to your id and then async find your data.
ParseQuery query = new ParseQuery("Manual_FriendList");
query.whereEqualTo("manual_frienID", the_id_you_want_to_search);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects,
ParseException e) {
if (e == null) {
//Success - do whatever you want with this user
} else {
Log.d("TAG", "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.

Delete a row from Parse.com using android studio

I have a table with two attributes: a name and a number. I have editext into which a user enters a name and I should be able to delete the row containing that name.
First of all use:
String text = edittext.getText().toString();
After that, use this query:
ParseQuery<ParseObject> query = ParseQuery.getQuery(TableName);
query.whereEqualTo("Name", text);
query.getInBackground(objectId, new GetCallback<ParseObject>() {
public void done(ParseObject object, ParseException e) {
if (e == null) {
object.deleteInBackground();
} else {
// something went wrong
}
}
});
If you need more info, go to: https://parse.com/docs/android/guide
private EditText txtDescription = (EditText) layout.findViewById(R.id.txtDescription)
String string = txtDescription.getText().toString();
Parse Object :
Storing data on Parse is built around the ParseObject. Each ParseObject contains key-value pairs of JSON-compatible data. This data is schemaless, which means that you don't need to specify ahead of time what keys exist on each ParseObject. You simply set whatever key-value pairs you want, and our backend will store it.
https://parse.com/docs/android/guide
String userName= edittext.getText().toString();
If userName contains multiple rows in parse,
To delete one by one use below code,
ParseQuery<ParseObject> query = ParseQuery.getQuery("your table name");
query.whereEqualTo("table_coloumn_name", userName);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject object : objects) {
try {
object.delete();
object.saveInBackground();
} catch (ParseException exe) {
exe.printStackTrace();
}
}
}
});
To delete all rows use below code,
ParseQuery<ParseObject> query = ParseQuery.getQuery("your table name");
query.whereEqualTo("table_coloumn_name", userName);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
ParseObject.deleteAllInBackground(objects, new DeleteCallback() {
#Override
public void done(ParseException e) {
Log.d("delted", "success");
}
});
}
});

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

Sort ParseQuery by boolean field

I want to sort my query first if a field is true or false, and then by updatedAt. How do I achieve this on Android with Parse SDK?
My current code is:
mQuery.orderByDescending("draft");
mQuery.addAscendingOrder("updatedAt");
But I get java.lang.IllegalArgumentException: Unable to sort by key draft.
First parse the query with draft values true with ascending order of updateTime and store the values to an array. After fetching this parse the query with draft values false.
ArrayList<ParseObject> result=new ArrayList();
ParseQuery<ParseObject> query = ParseQuery.getQuery("_User");
query.whereEqualTo("draft", true);
query.orderByAscending("updatedAt");
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
result=object;
ParseQuery<ParseObject> query1 = ParseQuery.getQuery("_User");
query1.whereEqualTo("draft", false);
query1.orderByAscending("updatedAt");
query1.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> object, ParseException e) {
if (object == null) {
Log.d("score", "The getFirst request failed.");
} else {
result=object;
for(int i=0;i<object.size();i++)
{
result.add(object.get(i));
}
}
}
});
}
}
});
Try using
mquery.whereEqualTo("True","draft");
instead of orderByDescending.

Categories

Resources