无法使用Java中的接口从Firebase数据库检索和存储数据 [英] Having trouble with interfaces in Java regarding retrieval and storage of data from a Firebase Database

查看:143
本文介绍了无法使用Java中的接口从Firebase数据库检索和存储数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



Inside mCheckInforInServer - >Posted text - > Took Value



如何更改我的代码以按以下顺序获取:



Inside mCheckInforInServer - >接受价值 - >发表文章

三项功能

  getServerTime.setValue(ServerValue.TIMESTAMP).addOnCompleteListener(new OnCompleteListener< Void>(){
@Override
public void onComplete(@NonNull Task< ; Void> task){

system_time = System.currentTimeMillis();
getServerTime.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
server_time_long = dataSnapshot.getValue(Long.class);


public interface OnGetDataListener {
public void onStart();

public void onSuccess(DataSnapshot data);

public void onFailed(DatabaseError databaseError);


public void mReadDataOnce(final OnGetDataListener listener){
listener.onStart();
$ b getServerTime = FirebaseDatabase.getInstance()。getReference(Users / Arjun / ServerTime);
getServerTime.addListenerForSingleValueEvent(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
listener.onSuccess(dataSnapshot);
Toast.makeText MainActivity.thisTook valueToast.LENGTH_LONG).show();
}
$ b @Override
public void onCancelled(DatabaseError databaseError){
listener .onFailed(databaseError);
}
});



$ b private void mCheckInforInServer(){
Toast.makeText(MainActivity.this,Inside mCheckInforInServer,Toast.LENGTH_LONG).show ();
$ b mReadDataOnce(new OnGetDataListener(){
@Override
public void onStart(){
//做一些事情,当开始获取数据时

}

@Override
public void onSuccess(DataSnapshot data){
//做一些事情,当获得数据成功时
server_time_display.setText(String。 valueOf(data));
Toast.makeText(MainActivity.this,Posted text,Toast.LENGTH_LONG).show();


}

@Override
public void onFailed(DatabaseError databaseError){
//做一些事情当数据失败时
Toast.makeText(MainActivity.this,Failed to post text, Toast.LENGTH_LONG).show();

}
});

我在调用函数 p>

我在按钮的on-click事件上调用函数:

  get_times.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view){





mCheckInforInServer();

}


解决方案因为 onDataChange()方法被称为异步,所以你不能改变显示这些Toast的顺序。这意味着甚至在你试图从数据库中获取数据之前,试图显示张贴的文本的Toast被调用,这就是为什么你要执行这个命令。

为了解决这个问题,你需要从数据库中得到的所有内容必须在 onDataChange()方法中使用,否则将会是 null



如果您想使用 onDataChange()方法之外的数据库中的数据,我建议您从信息


When I run the following code: I get the toast messages in the following order:

"Inside mCheckInforInServer" --> "Posted text" --> "Took Value"

How do I change my code to get it in the following order:

"Inside mCheckInforInServer" --> "Took Value" --> "Posted text"

The Three Functions

getServerTime.setValue(ServerValue.TIMESTAMP).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {

                    system_time = System.currentTimeMillis();
                    getServerTime.addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            server_time_long = dataSnapshot.getValue(Long.class);


public interface OnGetDataListener {
    public void onStart();

    public void onSuccess(DataSnapshot data);

    public void onFailed(DatabaseError databaseError);
}

public void mReadDataOnce(final OnGetDataListener listener) {
    listener.onStart();

    getServerTime = FirebaseDatabase.getInstance().getReference("Users/Arjun/ServerTime");
    getServerTime.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            listener.onSuccess(dataSnapshot);
            Toast.makeText(MainActivity.this, "Took value", Toast.LENGTH_LONG).show();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            listener.onFailed(databaseError);
        }
    });


}

private void mCheckInforInServer() {
    Toast.makeText(MainActivity.this, "Inside mCheckInforInServer", Toast.LENGTH_LONG).show();

    mReadDataOnce(new OnGetDataListener() {
        @Override
        public void onStart() {
            //DO SOME THING WHEN START GET DATA HERE

        }

        @Override
        public void onSuccess(DataSnapshot data) {
            //DO SOME THING WHEN GET DATA SUCCESS HERE
            server_time_display.setText(String.valueOf(data));
            Toast.makeText(MainActivity.this, "Posted text", Toast.LENGTH_LONG).show();


        }

        @Override
        public void onFailed(DatabaseError databaseError) {
            //DO SOME THING WHEN GET DATA FAILED HERE
            Toast.makeText(MainActivity.this, "Failed to post text", Toast.LENGTH_LONG).show();

        }
    });
}

Where I'm calling the function

I am calling the function on the on-click event of a button:

     get_times.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {





           mCheckInforInServer();

    }

解决方案

You cannot change the order of displaying those Toasts because onDataChange() method is called asynchronous which means that the Toast that you are trying to display Posted text is called even before you are trying to get the data from the database. That's why you are having that order of execution.

To solve this, all you need to get from the database must be used inside onDataChange() method otherwise will be null.

If you want to use the data from the database outside onDataChange() method, i suggest you see my answer from this post.

这篇关于无法使用Java中的接口从Firebase数据库检索和存储数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆