简体   繁体   English

根据部分 object 提供的数据,从部分 object 中检索 Firebase 数据

[英]Retrieve Firebase data from some object based on data provided by some object

数据库结构

I am new to Android Firebase.I have some app in which I have saved id's of all hotels in one object.我是 Android Firebase 的新手。我有一些应用程序,我在一个 object 中保存了所有酒店的 ID。 I have only id's there so based on those id's I want to retrieve all information which is inside some other object.我只有 id,所以根据这些 id,我想检索其他 object 中的所有信息。 What i have tried so far到目前为止我所尝试的

reference=FirebaseDatabase.getInstance().getReference().child(getResources().getString(R.string.all_countries)).child(country).child(city).child("Hotel");
    reference.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            hotels=new ArrayList<>();
            for (DataSnapshot dataSnapshot1:dataSnapshot.getChildren()){
                String id=dataSnapshot1.getKey();
                hotelIDs.add(id);
                reference1=FirebaseDatabase.getInstance().getReference().child("Hotel").child(id);
                reference1.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                        Hotel hotel=dataSnapshot.getValue(Hotel.class);
                        hotels.add(hotel);
                    }

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

                    }
                });
            }
//            }
            hotelsAdapter=new HotelsAdapterUser(hotels,UserViewHotels.this,"Hotel");
            hotelsRV.setAdapter(hotelsAdapter);

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {
            Log.e("iamhere",databaseError.getMessage());

        }
    });

I have all id's in hotelIDs array list and based on that when i try to get data my adapter is calling before the hotels array list is set up which means i am getting no data.Please help我在hotelIDs数组列表中有所有id,基于此,当我尝试获取数据时,我的适配器在设置酒店数组列表之前调用,这意味着我没有得到任何数据。请帮助

The easiest fix is to simply notify the adapter each time that you add a hotel to the list:最简单的解决方法是在每次将酒店添加到列表时简单地通知适配器:

reference1.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        Hotel hotel=dataSnapshot.getValue(Hotel.class);
        hotels.add(hotel);
        adapter.notifyDataSetChanged()
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {
        throw databaseError.toException();
    }
});

I made a few more changes here:我在这里做了一些改动:

  • Use addListenerForSingleValueEvent , so that we don't keep the listener attached.使用addListenerForSingleValueEvent ,这样我们就不会连接监听器。 With your original code, if you made a change to the hotel in the database, a second copy of that hotel would be added to the list.使用您的原始代码,如果您对数据库中的酒店进行了更改,则该酒店的第二个副本将添加到列表中。
  • Don't leave onCancelled empty, as you may be hiding important error messages there.不要将onCancelled留空,因为您可能在那里隐藏了重要的错误消息。 I implemented it in the simplest way possible here.我在这里以最简单的方式实现了它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM