Parse objects are not deleting - android

I am using parse as baas , I was using below code to delete objects .But tommorow i came to know that this piece of code is not working .
#Override
public void onClick(View view) {
new AlertDialog.Builder(context).setIcon(R.drawable.main).setTitle("Delete this product ?")
.setMessage("Are you sure you want to Delete this product ?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("VendorInv");
query.whereEqualTo("objectId", details.getObjectId());
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
try {
object.delete(); // fixed part
object.saveInBackground(new SaveCallback() { // fixed part
#Override
public void done(ParseException e) {
if (e == null) {
vendorDetailsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, vendorDetailsList.size());
count.setText(String.valueOf(vendorDetailsList.size()) + "/" + totalNo + " items are Online");
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
}
}
});
} catch (ParseException e1) {
Log.i("sand", e1.toString());
}
}
});
}
}).setNegativeButton("No", null).show();
}
});
}
Then as a contributor of parse android said than i fixed this part of code below
holder.cross.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(context).setIcon(R.drawable.main).setTitle("Delete this product ?")
.setMessage("Are you sure you want to Delete this product ?").setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
ParseQuery<ParseObject> query = ParseQuery.getQuery("VendorInv");
query.whereEqualTo("objectId", vendorDetailsList.get(position).getObjectId());
query.getFirstInBackground(new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
/* this part is changed */ object.deleteInBackground(new DeleteCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("sand", vendorDetailsList.get(position).getObjectId());
vendorDetailsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, vendorDetailsList.size());
count.setText(String.valueOf(vendorDetailsList.size()) + "/" + totalNo + " items are Online");
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Not Deleted please check your network connection", Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}).setNegativeButton("No", null).show();
}
});
Toast is printing deleted but still object is not deleted in database.
I dont know where i am going wrong, but this is not helping me in deleting an object
Any help would be greatly appreciated

Config your class VendorInv on the server to be "Public Read and Write enabled"
and use next code:
ParseQuery<ParseObject> query = ParseQuery.getQuery("VendorInv");
query.getInBackground(vendorDetailsList.get(position).getObjectId(), new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (object != null) {
object.deleteInBackground(new DeleteCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
Log.i("sand", vendorDetailsList.get(position).getObjectId());
vendorDetailsList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position, vendorDetailsList.size());
count.setText(String.valueOf(vendorDetailsList.size()) + "/" + totalNo + " items are Online");
Toast.makeText(context, "Deleted", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Not Deleted please check your network connection", Toast.LENGTH_SHORT).show();
}
}
});
}
}
});

Related

Database not update the value

I am trying to make a social app .. and I want when the user clicks the edit category database should be updated to the new value .. but it is like static not changing to new value .. I don't know the problem in logic or in the code ..
private void showCategoryUpdateDialog(String Key) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("تغيير " + Key);
LinearLayout linearLayout = new LinearLayout(getActivity());
linearLayout.setOrientation(LinearLayout.VERTICAL);
linearLayout.setPadding(15, 10, 15, 10);
RadioButton radioButton = new RadioButton(getActivity());
radioButton.setText("علمي علوم");
RadioButton radioButton2 = new RadioButton(getActivity());
radioButton2.setText("علمي رياضة");
RadioButton radioButton3 = new RadioButton(getActivity());
radioButton3.setText("أدبي");
linearLayout.addView(radioButton);
linearLayout.addView(radioButton2);
linearLayout.addView(radioButton3);
builder.setView(linearLayout);
builder.setPositiveButton("Update", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String value = radioButton.getText().toString().trim();
String value2 = radioButton2.getText().toString().trim();
String value3 = radioButton3.getText().toString().trim();
if (!TextUtils.isEmpty(value)) {
pd.show();
HashMap<String, Object> result = new HashMap<>();
result.put(Key, value);
databaseReference.child(user.getUid()).updateChildren(result)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
pd.dismiss();
Toast.makeText(getActivity(), "Updated...", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
pd.dismiss();
Toast.makeText(getActivity(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(getActivity(), "Please enter " + Key, Toast.LENGTH_SHORT).show();
}
if (!TextUtils.isEmpty(value2)) {
pd.show();
HashMap<String, Object> result2 = new HashMap<>();
result2.put(Key, value2);
databaseReference.child(user.getUid()).updateChildren(result2)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
pd.dismiss();
Toast.makeText(getActivity(), "Updated...", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
pd.dismiss();
Toast.makeText(getActivity(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(getActivity(), "Please enter " + Key, Toast.LENGTH_SHORT).show();
}
if (!TextUtils.isEmpty(value3)) {
pd.show();
HashMap<String, Object> result3 = new HashMap<>();
result3.put(Key, value3);
databaseReference.child(user.getUid()).updateChildren(result3)
.addOnSuccessListener(new OnSuccessListener<Void>() {
#Override
public void onSuccess(Void aVoid) {
pd.dismiss();
Toast.makeText(getActivity(), "Updated...", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
pd.dismiss();
Toast.makeText(getActivity(), "" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
} else {
Toast.makeText(getActivity(), "Please enter " + Key, Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
In this picture have more details
Emulator
And this GIF from my Database
https://media.giphy.com/media/ZY36QuzJsdXIhIQh7k/giphy.gif
first: make sure that the new value is stored in the database (from the console)
second: after the update is completed onCompleteListener get the data again.
you can listen for this field changes in real-time which could be better than getting the data after every update.

Parse Frame Work Update Query does not work

I am using parse frame work in android, I am updating user object in that but I update that old data is replaced with new data here my code is . How to resolve it any one help me solve out it.
ParseQuery<ParseUser> query = ParseUser.getQuery();
query.whereEqualTo("objectId", currentUser.getObjectId());
query.findInBackground(new FindCallback<ParseUser>() {
#Override
public void done(List<ParseUser> objects, ParseException e) {
currentUser.put("forbiddenWords", wordArray);
currentUser.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
hideProgressDialog();
if (e == null) {
Snackbar.make(parentView, R.string.string_setting_chnaged, Snackbar.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});
}
});
I guess you wanna update some value of your current user. Try this code
ParseUser user = ParseUser.getCurrentUser();
user.put("forbiddenWords", wordArray);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
hideProgressDialog();
if (e == null) {
Snackbar.make(parentView, R.string.string_setting_chnaged, Snackbar.LENGTH_LONG).show();
} else {
e.printStackTrace();
}
}
});

Android Parse.com Unable to remove an item from Array field in both installation and User object

I've two array fields in my parse app one in Installation object named as "subscriber" and other in User object named as "SubsUser".
I've been trying to remove an item from both arrays by using this code...
user.getList("SubsUser").remove(item);
user.save();
installation.getList("subscriber").remove(item);
installation.save();
but even after using this code the items are visible on parse dashboard, it seems both my tables are not being updated.
You should use put method to let ParseObject aware data is dirty.
ParseUser user = ParseUser.getCurrentUser();
List<Object> list = user.getList("SubsUser");
list.remove(item);
user.put("SubsUser", list);
user.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d(TAG, "saved in background");
if (e == null)
//ok
else
//something else went wrong
}
});
I myself solved the problem, i was using incorrect code
the write code to remove an object or collection of object from a parse array is
List<String> list=new ArrayList<String>();
list.add(item);
user.removeAll("SubsUser",list);
user.save();
installation.removeAll("subscriber",list);
installation.save();
I have a solution too, with a dialog
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.dialogo_si_no);
dialog.setTitle("La Lista");
Button btnAceptar = dialog.findViewById(R.id.btnSi);
Button btnCancelar = dialog.findViewById(R.id.btnNo);
TextView lblMensaje = dialog.findViewById(R.id.lblMensaje);
lblMensaje.setText("¿Deseas eliminar este evento de tus favoritos?");
dialog.show();
btnAceptar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
RelativeLayout layout = (RelativeLayout) view.getParent();
TextView lblObjectId1 = (TextView)layout.getChildAt(0);
final String objectId = lblObjectId1.getText().toString();
final ParseQuery<ParseObject> qFavoritos = ParseQuery.getQuery("favoritos").whereEqualTo("usuarioId",sharedpreferences.getString("globalUserId","0"));
try {
List<ParseObject> resultado = qFavoritos.find();
final String id = resultado.get(0).getObjectId();
qFavoritos.getInBackground(id, new GetCallback<ParseObject>() {
#Override
public void done(final ParseObject object, ParseException e) {
if (e == null) {
List<Object> lista = object.getList("eventos");
lista.remove(objectId);
object.put("eventos",lista);
object.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
Log.d(TAG, "saved in background");
if (e == null) {
Log.d(TAG, "favorito eliminado");
Intent intent = new Intent(activity, EventosActivity.class);
activity.startActivity(intent);
}else{
Log.d(TAG, e.getMessage());
}
}
});
}else{
Log.d(TAG,e.getMessage());
}
}
});
}catch (Exception ex){
Log.d(TAG,ex.getMessage());
}
}
});
btnCancelar.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});

Parse.com not saving ParseObjects

I am trying to upload file to Parse cloud.
final ParseFile parseFile = new ParseFile("somefile.png", data);
parseFile.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null){
ParseObject parseObject = new ParseObject("photo");
parseObject.setObjectId("someId");
parseObject.put("photo1", parseFile);
parseFile.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e==null){
Log.d(TAG, "Save done");
} else {
Log.d(TAG, "ex" + e.getMessage());
}
}
});
} else {
Log.d("USER", "file save ex" + e.getMessage());
}
}
});
This will run and show successfully and show "Save done" log. But when i go to Parse dashboard i don't see anything changing in data.
I tried to retrieve object:
ParseQuery<ParseObject> query = ParseQuery.getQuery("photo");
query.getInBackground("someId", new GetCallback<ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null){
Log.d("USER", "OK");
} else {
Log.d("USER", "ex" + e.getMessage());
}
}
});
But i get ParseException: no results found for query.
Looks like you are saving the parseFile twice and forgetting to save the parse object that you placed the file in.
change this
parseFile.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e==null){
Log.d(TAG, "Save done");
} else {
Log.d(TAG, "ex" + e.getMessage());
}
}
});
to this
parseObject.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e==null){
Log.d(TAG, "Save done");
} else {
Log.d(TAG, "ex" + e.getMessage());
}
}
});
I think that you would to do something like this:
// Create the ParseFile
ParseFile parseFile = new ParseFile("somefile.png", data);
file.saveInBackground();
ParseObject imgupload = new ParseObject("photo");
imgupload.put("ImageName", "somefile");
imgupload.put("photo1", parseFile);
imgupload.saveInBackground();
So, your query:
ParseQuery<ParseObject> query = ParseQuery.getQuery("photo");
query.whereEqualTo("ImageName", "somefile");
query.findInBackground(new FindCallback <ParseObject>() {
#Override
public void done(ParseObject object, ParseException e) {
if (e == null){
Log.d("USER", "OK");
} else {
Log.d("USER", "ex" + e.getMessage());
}
}
});

ParseObject not updated after trying to update/sve it in android

I am trying to retrieve a row in my table on parse. I found the objectId, but when I try to update the row with new information, nothing happens. Any help would be appreciated. Thanks!
mSaveAndNextSteps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseQuery<SingleFactInfo> query = ParseQuery.getQuery("Fact_Info");
query.getInBackground(mEventObjectId, new GetCallback<SingleFactInfo>() {
public void done(ParseObject fact, ParseException e) {
if (e == null) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the Parse Cloud. playerName hasn't changed.
fact.add("factDescriptiontwo", mEventDescription.getText().toString());
event.put("factDescription", mEventDescription.getText().toString());
event.saveInBackground(new SaveCallback() {
#Override
public void done(com.parse.ParseException e) {
}
});
}
}
#Override
public void done(SingleFactInfo parseObject, com.parse.ParseException e) {
Intent goToFactLists = new Intent(CreateEventSectionTwo.this, EventList.class);
startActivity(goToFactLists);
}
});
}
});
So here is the working solution that worked for me. Queried the object using objectId, then updated it in the 'done' section.
mSaveAndNextSteps.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ParseQuery<SingleFactInfo> query = new ParseQuery("Event_Info");
query.getInBackground(mEventObjectId, new GetCallback<SingleFactInfo>() {
public void done(ParseObject event, ParseException e) {
if (e == null) {
// Now let's update it with some new data. In this case, only cheatMode and score
// will get sent to the Parse Cloud. playerName hasn't changed.
Toast.makeText(getApplicationContext(), mYourEditText.getText().toString(), Toast.LENGTH_LONG).show();
}
}
#Override
public void done(SingleFactInfo parseObject, com.parse.ParseException e) {
parseObject.put("eventDescription", mYourEditText.getText().toString());
parseObject.saveInBackground();
Intent goToFactsList = new Intent(YourApplication.this, EventList.class);
startActivity(goToFactsList);
}
});
}
});

Categories

Resources