解析对象不会被删除

时间:2017-08-25 06:10:02

标签: android parse-platform

我使用parse作为baas,我使用下面的代码来删除对象。但是tommorow我知道这段代码不起作用。

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


        }

    });

}  

然后作为解析android的贡献者说比我修复了下面这部分代码

    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正在打印已删除,但仍未在数据库中删除对象。 我不知道我哪里出错了,但这并没有帮我删除一个对象 任何帮助将不胜感激

1 个答案:

答案 0 :(得分:0)

将服务器上的类VendorInv配置为“#34;启用公共读取和写入”#34; 并使用下一个代码:

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