Subhajit Das
Subhajit Das

Reputation: 106

Read error in Firebase Database Android

I have stored a value in a node Last-book-ID ="-1" when ever I try to read it the value is always shown as 0 why ? can anyone suggest where am I am going wrong People please help The JSONTree The code used to fetch the data

Upvotes: 0

Views: 161

Answers (1)

dfinn
dfinn

Reputation: 1008

The problem is that your method getLastBookId() will always return the value of bid[0] which has not yet been initialized to anything. If you're displaying that, it will always be 0.

When you call addListenerForSingleValueEvent(), it's setting up an asynchronous callback that will happen at some later point in time, after the database query has completed. So it most likely will not yet have populated bid[0] by the time your method returns.

You need to change your thinking and probably your app design a bit to work in line with this... for example, at the end of the onDataChange() callback, make the UI update to display the value where it's needed, etc.

Upvotes: 2

Related Questions