简体   繁体   中英

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. I have only id's there so based on those id's I want to retrieve all information which is inside some other 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

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. 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. I implemented it in the simplest way possible here.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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