简体   繁体   English

Firebase Android中的读取错误

[英]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 ? 当我尝试读取它时,我已经在节点Last-book-ID ="-1"存储了一个值,该值始终显示为0,为什么? can anyone suggest where am I am going wrong People please help 任何人都可以建议我要去哪里错人请帮助 JSONTree用于获取数据的代码

The problem is that your method getLastBookId() will always return the value of bid[0] which has not yet been initialized to anything. 问题是您的方法getLastBookId()将始终返回尚未初始化为任何东西的bid [0]的值。 If you're displaying that, it will always be 0. 如果要显示,它将始终为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. 当您调用addListenerForSingleValueEvent()时,它将设置一个异步回调,该回调将在数据库查询完成后的某个稍后时间发生。 So it most likely will not yet have populated bid[0] by the time your method returns. 因此,到您的方法返回时,很可能尚未填充bid [0]。

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. 您需要改变思维方式,也许您的应用程序设计需要与此配合才能工作……例如,在onDataChange()回调的末尾,使UI更新以在需要的地方显示值,等等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM