无法使用FirebaseUI从FirebaseDatabase Realtime调用值 [英] Unable to call values from FirebaseDatabase Realtime Using FirebaseUI

查看:37
本文介绍了无法使用FirebaseUI从FirebaseDatabase Realtime调用值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以上只是FirebaseDatabase中文件夹结构的示例. 我的查询是我应该如何获取那些子节点值.

Above is just an example of the structure of the folder in FirebaseDatabase. My query is that how am I suppose to get those child node value.

下面是我尝试的代码:

//FirebaseDatabase Collecting all the Item List
    ValueEventListener listener= new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()){

                for (DataSnapshot ds: dataSnapshot.getChildren()){
                    arrayItemList.add(ds.getKey());
               }

                firebaseRecyclerViewItem(arrayItemList);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

            Log.i(TAG, "onCancelled: Unable to fetch data");

        }
    };
    lookUpDatabase.addValueEventListener(listener);

以下是方法firebaseRecyclerViewItem()的代码:

Here is code for the Method firebaseRecyclerViewItem():

public void firebaseRecyclerViewItem(final ArrayList<String> arrayItemList) {
    DatabaseReference reference = null;
    String referenceName = null;

    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            //FirebaseDatabase 2'nd Reference Address
            for (DataSnapshot ds : dataSnapshot.getChildren()) {

                String key1 = ds.getKey();
                Log.i(TAG, "onDataChange: " + key1);

                String firebaseItemName = ds.child("DATA").getValue(String.class);
                Log.i(TAG, "onDataChange: " + firebaseItemName);
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    };

    for (int i = 0; i < arrayItemList.size(); i++) {

        referenceName = arrayItemList.get(i);
        reference = FirebaseDatabase.getInstance().getReference().child("DATA").child(referenceName);
        reference.addValueEventListener(valueEventListener);

        //Loading RecyclerView
        RecyclerViewItemList(reference, referenceName);
    }

}

不是我无法获得这些值,但是我能够获得它,但是在我附加的数据库图片中有三个FILE01,FILE02和FILE03.我只能获取最新的FILE03,即"01"值,我的意思是ValueEventListner跳过那里存在的所有其他文件,仅获取最新的更新或最新的文件.但我希望在那里显示的所有文件数据都可以在我的recyclerView上显示.

Is not that I am not able to get the values, I am able to get it however in the database pic which I have attached there are three FILE01, FILE02 and FILE03. I am only able to get the last FILE03, "01" value, I mean ValueEventListner skips all the other FILE present there only take the Last update or the last file. But I want all the files data present there to display it on my recyclerView.

我还尝试了另一种方法来获取DataSnaphot,并使用for()循环,其中使用了嵌套循环:

I have also tried a different approached where I get the DataSnaphot, and use the for() Loop, where I used nested loop:

//FirebaseDatabase Collecting all the Item List
    ValueEventListener listener= new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

            if (dataSnapshot.exists()){

                // here is where the changes which I have made, I tried to called the value which is impossible but still I tried to, but not result.

                for (DataSnapshot ds: dataSnapshot.getChildren()){

                   String key= ds.getKey();
                    String firebaseItemName = ds.child("DATA").getValue(String.class);
                Log.i(TAG, "onDataChange: " + firebaseItemName);

                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

            Log.i(TAG, "onCancelled: Unable to fetch data");

        }
    };
    lookUpDatabase.addValueEventListener(listener);

    RecyclerViewItemList(lookUpDatabase);

有没有一种方法或方法可以用来调用值.

Is there a way or a method which I can use to call the values.

推荐答案

您应在 for循环中调用 notifyDataSetChanged().否则,您的引用 referenceName 将被重写.

You should call notifyDataSetChanged() in for loop. Otherwise your reference and referenceName will be rewrited.

因此您的代码应为

 for (int i = 0; i < arrayItemList.size(); i++){
//Add your data to arraylist

rvAdapter.notifyDataSetChanged();
}

修改:

这将显示您的Firebase数据.我只是改变了第二种方法.

This will show your firebase data. I just changed your second method.

/*I changed Here*/
 private ArrayList<HashMap<String,String>> yourChildList = new ArrayList<>();

 ValueEventListener listener= new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

        if (dataSnapshot.exists()){


            for (DataSnapshot ds: dataSnapshot.getChildren()){

                /*I changed Here*/
                HashMap<String,String> dataMap = new HashMap<>();


               String key= ds.getKey();
                String firebaseItemName = ds.child("DATA").getValue(String.class);
            Log.i(TAG, "onDataChange: " + firebaseItemName);


            /*I changed Here*/
            dataMap.put(key,firebaseItemName);
            yourChildList.add(yourDataMap);

            }


            /*I changed Here
            *
            *I think this method is used to update data to your recyclerView
            *if I'm right, you can replace it
            *RecyclerViewItemList(lookUpDatabase);
            *
            */ 

            yourRecyclerAdapter = new RecyclerAdapter(yourChildList);
            yourRecyclerAdapter.notifyDataSetChanged();
            recyclerView.setAdapter(yourRecyclerAdapter);

        }
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        Log.i(TAG, "onCancelled: Unable to fetch data");
    }
};
lookUpDatabase.addValueEventListener(listener);

注意:我不建议您使用嵌套的侦听器.您应避免使用嵌套的侦听器以获得更好的性能和可维护性.您应该使用观察者 LiveData 来更新Recyclerview.

Note:I don't recommend you to use nested listeners. You should avoid nested listener for better performance and maintainability. You should use Observer or LiveData to update Recyclerview.

这篇关于无法使用FirebaseUI从FirebaseDatabase Realtime调用值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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