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

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

问题描述

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

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:

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

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