updating values per row in parse.com using android

2019-07-15 18:56发布

问题:

I have a problem with updating values per row in a cloud storage site called parse.com. I am only a newbie in using parse.com. I have read the documentation about it and have understand it. But what I want to do is a little bit different from the example there. Here's my code..

public void onClick(View arg0) {
    if (arg0.getId() == R.id.button1) {
        ParseQuery query = new ParseQuery("inventory");
        query.findInBackground(new FindCallback() {

              public void done(List<ParseObject> test, ParseException e) {

                  ParseObject testObject = new ParseObject("inventory");
                  if(e==null) { 
                      String str="";
                      String str2="";
                      for(int x =0;x<test.size();x++){
                          str = test.get(x).getString("name");
                          str2 = test.get(x).getString("quantity");
                          if (!str.equals(et1.getText().toString())){
                             str = "";
                          }
                          if(str.equals(et1.getText().toString()))
                          {


                                      testObject.put("quantity", et2.getText().toString());
                                      testObject.saveInBackground();

                              x = test.size();
                          }
                          else{
                              tv1.setText("error" + e.getMessage());
                          }
              }


                  }}

        });

    } }

I want to update the quantity of the product name that i have inputted. when my input is equal to the name on the cloud it will update the other column which refers to the product name that I have inputted. But as a result of my code, it creates a new row, instead of updating the existing row.. what is lacking/wrong in my code? can someone please help me? :) thanks in advance! :)

回答1:

I'm figuring out the same problem. I have found this:

The answer says to update an existing object, you need to retrieve it first. The retrieve method is given in the documentation on their website.

Edit: Done! Here is how you do it; first fetch the object and then update the values you want to through the update method.

ParseQuery query = new ParseQuery("UserName");
            query.getInBackground("GKIp4pxBrF", new GetCallback() {
              public void done(final ParseObject object, ParseException e) {
                if (e == null) {

                    object.put("Name", username);
                    object.saveInBackground(new SaveCallback() {
                    public void done(ParseException e) {

                    object.put("Name", oldname);

                }
              });

             }

                else { 
                 e.printStackTrace();
                }

        }
});

In case you don't know the object code before hand, I suggest you find the required row through a query.