Android无法从Firebase Datasnapshot返回值

时间:2018-07-17 10:18:58

标签: java android firebase arraylist firebase-realtime-database

在这里,我需要从名为“ news_resources”的“ Firebase”节点获取“ ArrayList”的值。 'INSIDE'日志显示'dataList1'的大小为1,但'OUTSIDE''Log'显示的'dataList1'的大小为0。因此'fireBaseDataList'为null。

MainActivity.java

private static List<AppDataBean>fireBaseDataList;

protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.activity_main);

   fireBaseDataList = getDataFromFirebase("stringUrl");

}

private List<AppDataBean> getDataFromFirebase(final String url)
    {
        final ArrayList<AppDataBean>dataList1 = new ArrayList<>();
        dataList1.clear();
        DatabaseReference reference = FirebaseDatabase.getInstance().getReference("news_resources");

        reference.orderByChild("url").equalTo(url).addListenerForSingleValueEvent(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot datas: dataSnapshot.getChildren()){
                    String country  = datas.child("country").getValue().toString();
                    String language = datas.child("language").getValue().toString();
                    String category = datas.child("category").getValue().toString();
                    String iconUrl = datas.child("iconUrl").getValue().toString();
                    String source = datas.child("source").getValue().toString();

                for(int i=0; i<datas.gretChildrenCount(); i++)
                {       
                    AppDataBean appDataBean = new AppDataBean();
                    appDataBean.setCountry(country);
                    appDataBean.setUrl(url);
                    appDataBean.setSource(source);
                    appDataBean.setIconUrl(iconUrl);
                    appDataBean.setLanguage(language);

                    dataList1.add(appDataBean);

                    Log.d("INSIDE","NEWS_RESOURCES_BEANS_ARE "+ dataList1.size() + ", " + appDataBean.getCountry() + ", " + appDataBean.getUrl()
                            + ", " + appDataBean.getSource()  + ", " + appDataBean.getIconUrl() + ", " + appDataBean.getLanguage());

                  }

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

        Log.d("OUTSIDE", "FIREBASE_DATA_LIST_SIZE_IS " + dataList1.size());
        return dataList1;
    }

0 个答案:

没有答案