从Override onDataChange保存字符串以在Android Firebase中查看外部信息 [英] Save String from Override onDataChange to see outside in Android Firebase

查看:199
本文介绍了从Override onDataChange保存字符串以在Android Firebase中查看外部信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据库的字符串变量。我有@Override onDataChange方法的访问。

但是我想看看这个@Override方法之外的字符串的值。现在,我看不到,因为从数据库中的这个字符串的值只能在@Override方法中使用。
$ b $ pre $ firebase.addValueEventListener( new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){

CarClass carClass = dataSnapshot.getValue(CarClass.class);

string1 = carClass.getCarName();
//string1的值是=name

}

@Override
public void onCancelled(FirebaseError firebaseError){


});
//这里string1的值是空的。

有人可以帮我解决这个问题吗?

编辑

我自己找到解决方案:

要从onDataChange方法访问string1的值,我使用了SharedPreferences。我把这个string1放在@Override方法中的SharedPreferences中,我可以使用SharedPreferences获得这个方法以外的值string1。 :

  public static final String Car_Name =Name_PREFS; 
public static final String Car_Key =String_PREFS;
SharedPreferences sharedPreferences;

firebase.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){

CarClass carClass = dataSnapshot.getValue CarClass.class);

string1 = carClass.getCarName();

$ b sharedPreferences = getApplicationContext()。getSharedPreferences(Car_Name,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(Car_Key,string1);
editor.apply();


}

@Override
public void onCancelled(FirebaseError firebaseError){


});


sharedPreferences = getApplicationContext()。getSharedPreferences(Car_Name,MODE_PRIVATE);
string1 = sharedPreferences.getString(Car_Key,null);
//这里string1的值存在于onDataChange方法之外。
// string1 =name
}


解决方案让我复制我的答案从这个问题(请注意,问题不完全一样,但有类似的答案)

让我通过描述你的代码如何不工作预期:
$ b $ pre $ firebase.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(DataSnapshot dataSnapshot){
...(point 1)
}
$ b $ @Override
public void onCancelled(DatabaseError databaseError){}
}); Firebase数据库会异步加载您的数据。您可能会收到以下消息: 。这意味着(使其简单)获取数据的过程不会干扰您的主要过程。考虑到这一点,(point 2)中的代码并不总是在(point 1) ,反之亦然。虽然在(point 1)(point 2)后执行的代码通常是执行的,但并不总是这样。所以你应该考虑在(point 1)中写的代码可以在任何时候被执行。



然后你应该用这个概念写你的代码。这意味着如果你想对(point 1)(比如获取 string1 )的值的变量做任何事情,你应该把它放在(点1)



希望这个帮助。

注意: JP Ventura 在评论中提到(来自链接的问题)(point 2)总是在(point 1)作为回调 onDataChange 将花费至少4毫秒来运行

I've a String variable from a database. I've an access in @Override onDataChange method.

But I'd like to see that value of this string in outside of this @Override method. At now, I cannot see, because value of this string from database is available only in @Override method.

    firebase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {

                    CarClass carClass = dataSnapshot.getValue(CarClass.class);

                    string1 = carClass.getCarName();
                    // value of "string1" is = "name"

                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {

                }
            });
// here value of "string1" is empty.

Does someone could help me to resolve this problem?

EDIT:

I found solution myself:

To have an access of value of "string1" from "onDataChange method" I used SharedPreferences. I put this "string1" to SharedPreferences in @Override method, and I can get this value of "string1" outside of this method using SharedPreferences.

I show my explanation below:

     public static final String Car_Name = "Name_PREFS";
     public static final String Car_Key = "String_PREFS";
     SharedPreferences sharedPreferences;

      firebase.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {

                            CarClass carClass = dataSnapshot.getValue(CarClass.class);

                            string1 = carClass.getCarName();


    sharedPreferences = getApplicationContext().getSharedPreferences(Car_Name, Context.MODE_PRIVATE);
       SharedPreferences.Editor editor = sharedPreferences.edit();
                    editor.putString(Car_Key, string1);
                    editor.apply();


                        }

                        @Override
                        public void onCancelled(FirebaseError firebaseError) {

                        }
                    });


    sharedPreferences = getApplicationContext().getSharedPreferences(Car_Name, MODE_PRIVATE);
            string1 = sharedPreferences.getString(Car_Key, null);
            // here value of "string1" exists outside of "onDataChange" method.
// string1 = "name"
    }

解决方案

Let me copy my answer from this question here: (Note that the question is not exactly the same but have similar answer)

Let me make it more simple by describing how your code doesn't work as you expected:

    firebase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            ... (point 1)
        }

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

    ... (point 2)

Firebase Database load your data asyncronously. It means (to make it simple) that the process of getting data does not interfere with your main process. With that in mind, the code in (point 2) does not always get executed after code in (point 1), and vice versa. Though usually code in (point 1) get executed after (point 2), it is not always like that. So you should consider that the code you write in (point 1) can be executed at anytime.

Then you should write your code with that concept. Meaning that if you want to do anything to a variable inside (point 1) (like get value of string1), you should place it all inside (point 1)

Hope this help.

Note: JP Ventura mention in the comment (from linked question) that (point 2) is always executed after (point 1) as the callback onDataChange will take at least 4 milliseconds to run

这篇关于从Override onDataChange保存字符串以在Android Firebase中查看外部信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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