Delete a row from Parse.com using android studio - android

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

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

SaveAllInBackground doesn't work inside deleteAllInBackground as desired

SaveAllInBackground doesn't work inside deleteAllInBackground as desired.
I am trying to save a list of parseobjects using save all in background. To avoid duplicates in the table, I am querying for the already existing rows and deleting them if any and then save the new copy. Therefore I am calling the saveAllInBackground inside the deleteAllInBackground's callback.
The problem is this :
For ex: if the list to delete contains [a,b,c,d] and the list to upload has [a,b,c,d,e,f] only [e,f] get persised to parse. I am passing [a,b,c,d,e,f] to the saveAllInBackground but only [e,f] get persisted.
Is there something I am missing? How to solve this?
Can I use a different approach?
Is there a better way to avoid duplicates? I dont want to add a
beforeSave hook. The whole purpose of calling the saveAll is to reduce the number of API calls. I guess if I use beforeSave, I will have to run some queries in the cloud code anyway.
This is my code
ParseQuery query = new ParseQuery("PostChoice");
query.fromPin();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> localList, ParseException e) {
if (localList != null && !localList.isEmpty()) {
List<ParseObject> postList = new ArrayList<ParseObject>();
for (ParseObject object : localList) {
postList.add(object.getParseObject("post"));
}
ParseQuery query = new ParseQuery("PostChoice");
query.whereContainedIn("post", postList);
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> parseCloudList, ParseException e) {
if (parseCloudList != null && !parseCloudList.isEmpty()) {
ParseObject.deleteAllInBackground(parseCloudList, new DeleteCallback() {
#Override
public void done(ParseException e) {
// this gets executed and rows are accordingly deleted
ParseObject.saveAllInBackground(localList, new SaveCallback() {
#Override
public void done(ParseException e) {
// this gets executed but the rows are not uploaded.
//the locallist is not empty. it contains the right data.
editor.putLong(Four.LAST_CHOICE_SYNC_TIME, System.currentTimeMillis());
editor.commit();
Log.i("SyncChoiceService", "Synced Choices");
}
});
}
});
}
else{
ParseObject.saveAllInBackground(localList, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.i("SyncChoiceService", "Synced Choices");
editor.putLong(Four.LAST_CHOICE_SYNC_TIME,System.currentTimeMillis());
editor.commit();
}
});
}
}
});
}
}
});
I have come up with a solution like this. and it meets my requirement. I use the updatedValue and delete the old ones and the remaining get updated as a whole list.
ParseQuery query = new ParseQuery("PostChoice");
query.fromPin();
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> localList, ParseException e) {
if (localList != null && !localList.isEmpty()) {
List<ParseObject> postList = new ArrayList<ParseObject>();
for (ParseObject object : localList) {
postList.add(object.getParseObject("post"));
}
ParseQuery query = new ParseQuery("PostChoice");
query.whereContainedIn("post", postList);
query.whereLessThan("updatedAt",System.currentTimeMillis());
query.whereEqualTo("user", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(final List<ParseObject> parseCloudList, ParseException e) {
if (parseCloudList != null && !parseCloudList.isEmpty()) {
ParseObject.deleteAllInBackground(parseCloudList, new DeleteCallback() {
#Override
public void done(ParseException e) {
Log.i("SyncChoiceService", "Deleted old Choices");
}
});
}
}
});
ParseObject.saveAllInBackground(localList, new SaveCallback() {
#Override
public void done(ParseException e) {
Log.i("SyncChoiceService", "Synced Choices");
}
});
}
}
});
Yes, if i understand you correctly you want to save only new data from localdb to parse backend.
Best and less request solution would be to have anther field in your table called "Draft" or "isUpdated" (name as you want). Role of this flag is to identify whether this field is saved in backend or not. if its a new field "isUpdated" is false else its true. Then in query you can query only the isUpdated is false. Then save them in backend. Then
You don't want to delete any data.
Reduce requests
Less unnecessary logic in your code.
it's clean
Hope this helps

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.

get objectId parse android

I have done storing into database and but when I wish to retrieve the database elements, I always need to specify the objectID which needs to be checked in database. I want a way by which I can get objectID from the element or better get the object id while storing.
"o1l5gCCPB4" is the objectID
Here is code I used for retrieval:
query.getInBackground("o1l5gCCPB4", new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
// TODO Auto-generated method stub
if (e == null) {
String playerName = object.getString("foo");
tv.setText(playerName);
} else {
// something went wrong
tv.setText("Something went wrong!!");
}
}
});
You can get an element, this way:
ParseQuery<ParseObject> query = new ParseQuery<ParseObject>(PARSE_CLASS_DRAGS);
query.whereEqualTo("objectId", objectId);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> list, ParseException e) {
//convert the parseObjects to your objects
}
});
If you want to get all of the elements, you only have to ignore the query.whereEqualTo() row.

Add two users to parse object and Query

I want to create a parse object with two users and query for the object using both users. My get other user method returns the other user i want to add to the group.
String id;
final ParseUser[] user = new ParseUser[1];
public void getOtherUser()
{
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("username", "amanda");
query.findInBackground(new FindCallback<ParseUser>() {
public void done(List<ParseUser> objects, ParseException e) {
if (e == null) {
user[0] = objects.get(0);
addUserGroup();
} else {
// Something went wrong.
}
}
});
}
my add userGroup created a parse object with both users
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
After ive done this i want to be able to be able to update the parse object with new content and query for the object based on both users. The following mehtod is breaking on me though and im not sure why.
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
Its returning the following error
java.lang.IllegalArgumentException: invalid type for ParseObject: class [Lcom.parse.ParseUser;
anybody know why this might be happening?
When you call ParseUser.getCurrentUser() parse return a user object. Now assuming you want to query usernames you need to change the ParseUser.getCurrentUser() to ParseUser.getCurrentUser().getUsername()
private void addUserGroup()
{
final ParseObject group = new ParseObject("UserGroup");
group.put("from", ParseUser.getCurrentUser().getUsername());
group.put("to", user[0]);
group.saveInBackground((new SaveCallback() {
public void done(ParseException e) {
id = group.getObjectId();
}
}));
}
private void getData()
{
final double[] result = {0};
ParseQuery<ParseObject> query = ParseQuery.getQuery("UserGroup");
query.whereEqualTo("from",ParseUser.getCurrentUser().getUsername() );
query.whereEqualTo("to", user);
query.findInBackground(new FindCallback<ParseObject>() {
public void done(List<ParseObject> commentList, ParseException e) {
Log.d("found", "woohoo");
}
});
}

Categories

Resources