Android Studio Firebase数据库如何在调用中获取另一个类的值

时间:2018-10-16 11:47:14

标签: android firebase firebase-realtime-database

我想知道如何在onDataChange内的此调用for中获得另一个类的值。 在每次for的呼叫中,我都会使用数据“ Type Ad”和“ IdAd”进行呼叫。

在每个孩子的for (DataSnapshot ds: dataSnapshot.getChildren ()) {内,我想在类Type Ad(DatabaseReference usersRef = rootRef.child("TypeAd"))中取一个值(与从第一次调用中获得的DatabaseReference usersRef = rootRef.child("Usuarios").child(IdUser).child("Chats"); )的调用类不同。谢谢)非常感谢您的帮助

我的活动:

private void ConversUsersData() {

        RefreshList();//Resetea y no vuelve a printar
        //set progres bar
        showLoading.setVisibility(View.VISIBLE);
        DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
        DatabaseReference usersRef = rootRef.child("Usuarios").child(IdUser).child("Chats");
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {

                for (DataSnapshot ds : dataSnapshot.getChildren()) {
                    String key = ds.getKey();
                    String TypeAd = ds.child("TypeAd").getValue(String.class);
                    String IdAd = ds.child("IdAd").getValue(String.class);
                    String IdOtherUser = ds.child("IdOtherUser").getValue(String.class);
                    String nameOtherUser = ds.child("nameOtherUser").getValue(String.class);
                    String nameAd = ds.child("nameAd").getValue(String.class);
                    Boolean readBool =  ds.child("read").getValue(boolean.class);

                    //Log.i(idadname, "onDataChange: ");
                    //crea modelo usuariochat
                    ConversChat userchat = new ConversChat(key, IdAd, IdOtherUser, TypeAd, "08-03-1987",nameAd ,nameOtherUser, readBool);
                    ConversChatsList.add(userchat);

                }//posar el card view i adapter
                if (!ConversChatsList.isEmpty()) {
                    usersList.setVisibility(View.VISIBLE);
                    MyChats_Adapter adapter = new MyChats_Adapter(getActivity(), R.layout.adapter_chatlist, ConversChatsList);
                    usersList.setAdapter(adapter);
                    //usersList.setAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, ConversChatsList));

                } else {
                    usersList.setVisibility(View.GONE);
                    no_chat.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        };
        usersRef.addListenerForSingleValueEvent(eventListener);
        showLoading.setVisibility(View.INVISIBLE);
    }

1 个答案:

答案 0 :(得分:0)

只需在for循环内进行另一个查询