pavany
pavany

Reputation: 219

firebase retrieve complex structure

Hello I am new to Firebase database. Currently I am developing a android application. I am looking for a help, Below is my Firabase Data Structure. Can anyonce please tell me how to retrive the data.

Requirement is I have to retrieve the all the companies. Upon selecting the a specific company, it has to fetch the corresponding company coordinates into polygon shape variable.

Please Help

enter image description here

Upvotes: 0

Views: 1121

Answers (3)

Paras Rawat
Paras Rawat

Reputation: 29

Basically I got the answer from repetitive hits and tries while working on the similar problem.

Look when you go for general approach of add addValueEventListner(), you get a snapshot of the database with respect to your root node. Thus on the first addValueListner() callback, get the reference of your child node and then addChildEventListner() on it. So here is what i suggest:

 DatabaseReference databaseReference=FirebaseDatabase.getInstance().getReference();

 databaseReference.addValueEventListner(new ValueEventListner(){
 @Override
 public void onDataChange(@NonNull DataSnapshot dataSnapshot){

//here your snapshot is considered with respect to root. So you get the reference of child node now

 datasnapshot.child("Bureau 507").getRef().addChildEventListner(new 
 ChildEventListner){
  @Override
 public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s){
  for(DataSnapshot data: dataSnapshot.getChildren()){
  //Now you are at the subtree of Bureua 507. So now you get your data in the form of 
  key and values
  } 

//remaining callbacks

Upvotes: 0

VikasGoyal
VikasGoyal

Reputation: 3376

public class CompanyIds{
   public List<Company> companies;
}


FirebaseDatabase.getInstance()
                .getReference("parent node path of these ID's");

Now in FirebaseRecyclerAdapter what you can do is:-

@Override
    protected void populateViewHolder(final MessagesHolder viewHolder, CompanyIds model, int position) {
        Company = model.companies.get(0);
        //Set data on your view from Company model
    }

As your ID is having only one company then you get that only company from the first index.

Now after clicking on any item, you can easily get the details for a company like coordinates or anything else in the same way you set the data in the adapter. It won't be a problem.

public class Company{
       List<String> Cordinates;
       List<Employees> Employees;
       //Define other fields
}

Upvotes: 1

Nick
Nick

Reputation: 136

Why not have a structure like:

companyName/info

Instead of

Firebase ID/Companyname/info

i.e use update or set instead of push

Upvotes: 1

Related Questions