How to update an existing data object in Android?

I am trying to implement a headline and ranking system in my application and have been unable to update the Rank of any of the entries (simple int + 1). I have tried using the generated methods as well as the code shown in the documentation but my objects never update.

Example code … (Headlines being the table)

 Headlines h = Headlines.findById(headline.getObjectId());
                            h.setRank(h.getRank()+1);
                            h.save();

causes: BackendlessException{ code: ‘Internal client exception’, message: ‘null’ }

methods:

    public static Headlines findById(String id) {
        return Backendless.Data.of(Headlines.class).findById(id);
    }
    public int getRank() {
        return Rank;
    }
    public void setRank(int Rank) {
        this.Rank = Rank;
    }
    public Headlines save() {
        return Backendless.Data.of(Headlines.class).save(this);
    }

My code:

headline.setRank(headline.getRank() + 1);
                            Backendless.Persistence.save( headline, new AsyncCallback<Headlines>() {
                                @Override
                                public void handleResponse(Headlines headlines) {
                                    Log.wtf("LoungeFragment", "updates successfully +1");
                                }


                                @Override
                                public void handleFault(BackendlessFault backendlessFault) {
                                    Log.wtf("LoungeFragment", "updates unsuccessfully +1 "+backendlessFault.toString());
                                }
                            });

My code comes back successful, but nothing actually updates in the data table. What am I doing wrong? Please Help!

Hi Justin,

At the top of your post you say you’re getting BackendlessException, but then later you say “My code comes back successful”, so is there an exception or not? )) If there is, could you please check what specific line causes the exception? Is it the retrieval of a Headline object or saving it?

Regards,
Mark

Hi Mark,

Thanks for the quick reply!

I have a like and a dislike method, one uses the functionality produced by the code generator (first 5 code bubbled), the other uses code that I have written based on examples in the documentation.

The generated code will produce an error and a crash. Exception details from logcat: BackendlessException{ code: ‘Internal client exception’, message: ‘null’ }

The crash happens in the save() method on the line:

        return Backendless.Data.of(Headlines.class).save(this);

The code based off of the examples will not crash but will return the message below and nothing gets updated (note: it seems to pick a different column everytime):

“Column with name post_Time already exists. Please, choose a unique name for the new column.”

Thanks in advance!

JR

Hi Justin,

Could you please let me know your application id and we’ll look into it from our end.

Regards,
Mark

Hey Mark,

App ID is: 4BD1A87E-937C-A7EE-FF87-05733C1A7800

Thanks for the help,

JR

Hi, Justin.

Try to use Async method with Callback parameter for update
(if you use Async method in main activity).

You could generate crud sample, run app and update some object.
And this operation will be successful.

Regards,
Kate.

Hello Kate, thanks for the reply. I am already trying to yse the Async method with the callback. The code of the method is below…

 headline.setRank(headline.getRank() - 1);
 headline.saveAsync(new AsyncCallback<Headlines>() {
      @Override
      public void handleResponse(Headlines headlines) {
           Log.wtf("LoungeFragment","updates successfully -1");
      }


      @Override
      public void handleFault(BackendlessFault backendlessFault) {
           Log.wtf("LoungeFragment", "updates unsuccessfully-1 " + backendlessFault.getCode() + backendlessFault.getMessage() + backendlessFault.getDetail());
      }
});

The result of using this call is the duplicate column issue described above. Last run returned code 8001 and message: “Column with name title already exists. Please, choose a unique name for the new column”

Hi, Justin.

Could you please also provide your Headlines.java?

We have reproduced this issue and we will fix this as soon as it possible. To work around this trouble rename your column names, they should start from lower case.

Hello Sergey, renaming the columns to start with all lowercase did not fix the issue, I am still getting duplicate ID messages. The Headlines.java class is attached (as a txt file).

Headlines.txt (5.66kB)

Are you getting duplicate ID or “Column with name title already exists. Please, choose a unique name for the new column”? Please provide exact message

Hello Sergey, the exact error I am getting is “Column with name post_Time already exists. Please, choose a unique name for the new column.” Though each run of the app sometimes the column name will change.

Hi, Justin.

We have solved this issue.

Regards,
Kate.