Munish Kapoor Munish Kapoor - 2 months ago 13
Android Question

Firebase Android Parsing ID issue FirebaseRecyclerAdapter

I am trying to parse following JSON using Firebase

{
"organizations" : {
"-KS5bLCjQmNSQZDIQkTE" : {
"about" : {
"address" : "teka",
"city" : "sns",
"country" : "South Africa",
"created_at" : 1474358502482,
"name" : "diverse",
"pincode" : "167377",
"state" : "punjab",
"utc" : "2016-09-20 08:01:41"
},
"department" : {
"-KS9LUpZWo5UqtWKJwJ8" : {
"address" : "best",
"name" : "test"
}
}
}
}
}


Here is my Java code

@IgnoreExtraProperties
public class Organizations {

About about;
Department department;

public String getDepartment() {
return department;
}

public static class Department{
public String address;
public String name;

public String getName() {
return name;
}

public String getAddress() {
return address;
}

}

}


FirebaseRecyclerAdapter

fbOrganizationRecycleAdapter = new FirebaseRecyclerAdapter<Organizations, OrganizationHolder>
(Organizations.class, R.layout.list_items_orgnization,OrganizationHolder.class,lastFifty)
{
@Override
protected void populateViewHolder(OrganizationHolder viewHolder, final Organizations model, int position) {
Log.e(TAG, "test " + model.getDepartment().getName());
}
};


Everything is parsing fine except Department

It returning me null department when I am trying to get model.getDepartment().getName();

if data is like following JSON It's working fine

{
"organizations" : {
"-KS5bLCjQmNSQZDIQkTE" : {
"about" : {
"address" : "teka",
"city" : "sns",
"country" : "South Africa",
"created_at" : 1474358502482,
"name" : "diverse",
"pincode" : "167377",
"state" : "punjab",
"utc" : "2016-09-20 08:01:41"
},
"department" : {

"address" : "best",
"name" : "test"

}
}
}
}


I think the issue with ID. Please let me know

How to parse ID "-KS9LUpZWo5UqtWKJwJ8" of following JSON

"department" : {
"-KS9LUpZWo5UqtWKJwJ8" : {
"address" : "best",
"name" : "test"
}
}

Answer

Try this:

class DepartmentChild{
   public String address;
   public String name;

   public String getName(){
      return this.name;  
   }
}

public class Organizations  {
  public About about;
  public Map<String,DepartmentChild>  department;

  public Map<String, DepartmentChild> getDepartment() {
    return department;
  }

  public void setDepartment(Map<String, DepartmentChild> department){
    this.department = department;
  }
}

Example

void test(Organizations model){
   for (Map.Entry<String, DepartmentChild > entry : model. getDepartment().entrySet()) {
      Log.e(TAG, "test " + entry.getKey()+ "/" + entry.getValue().getName());
   }
}

You can iterate the Map like this:

for (Map.Entry<String, DepartmentChild> entry : department.entrySet())
{
  System.out.println(entry.getKey() + "/" + entry.getValue().getName());
}