从addValueEventListener Firebase中检索字符串 [英] Retrieve String out of addValueEventListener Firebase

查看:92
本文介绍了从addValueEventListener Firebase中检索字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从addValueEventListener()方法接收一个字符串,该方法用于从数据库Firebase转售数据.数据正确到达.

I want to receive a string from addValueEventListener() method I use to resell the data from the database Firebase. The data arrive correctly.

但是当确定要从该方法中取出字符串以在另一个方法中使用它时,它什么也不返回.

But when certain to get the string out of that method to use it in another, it returns nothing.

您有提示吗?

我已经尝试过putExtras,还故意创建了一个方法,但是没有用.

I already tried putExtras and also create a method on purpose but it did not work.

final DatabaseReference mPostReference = FirebaseDatabase.getInstance().getReference().child("user-daily").child(getUid()).child("2017-Year");
mPostReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        final ArrayList<String> labels = new ArrayList<String>();
        for (DataSnapshot data : dataSnapshot.getChildren()){
            final DailyItem dailyItem = data.getValue(DailyItem.class);
            labels.add(dailyItem.mese);



        }
        title.setText(labels.get(position));

        a = title.getText().toString();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(view.getContext(),"database error",
                Toast.LENGTH_SHORT).show();
    }
});

//this return null... why?
String title = a;

推荐答案

数据是从Firebase异步加载的.在您运行title = a时,尚未调用onDataChange方法.在调试器中设置一些断点来验证这一点,这是理解异步加载如何工作的关键.

The data is loaded from Firebase asynchronously. By the time you run title = a, the onDataChange method hasn't been called yet. Set some breakpoints in a debugger to verify this, it's key to understanding how asynchronous loading works.

解决方案是将问题的框架从首先获取对象,然后对标题进行blabla"改为开始获取对象;对象可用后,对标题进行blabla".

The solution is to reframe your problem from "first get the object, then do blabla with the title" to "start getting the object; once the object is available, do blabla with the title".

在代码中,它转换为:

final DatabaseReference mPostReference = FirebaseDatabase.getInstance().getReference().child("user-daily").child(getUid()).child("2017-Year");
mPostReference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        final ArrayList<String> labels = new ArrayList<String>();
        for (DataSnapshot data : dataSnapshot.getChildren()){
            final DailyItem dailyItem = data.getValue(DailyItem.class);
            labels.add(dailyItem.mese);



        }
        title.setText(labels.get(position));

        // Do blabla with the title
        String title = title.getText().toString();
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        Toast.makeText(view.getContext(),"database error",
                Toast.LENGTH_SHORT).show();
    }
});

许多刚接触Firebase(以及其他现代Web API,因为它们都以这种方式工作)的开发人员都在解决此问题.因此,我建议您也查看他们的问题和答案:

Many developers new to Firebase (and other modern web APIs, as they all work this way) struggle with this problem. So I recommend you also check out their questions and answers:

  • Cannot access firebaseObjectObservable outside of set
  • Android Firebase get value of child without DataChange
  • Value of a global variable is reset after it is initialised in ValueEventListener
  • can't get values out of ondatachange method
  • ArrayList not updating inside onChildAdded function
  • Setting Singleton property value in Firebase Listener
  • and most others in this list of search results

这篇关于从addValueEventListener Firebase中检索字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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