解析ID问题FirebaseRecyclerAdapter

时间:2016-09-21 01:43:10

标签: android firebase firebase-realtime-database firebaseui

我正在尝试使用Firebase解析以下JSON:

   {
          "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"
                }
              }
            }
          }
   }

这是我的Java代码:

@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());
                     }
                };

除了Department之外,所有内容都解析得很好。当我试图获得null department时,它会回复model.getDepartment().getName();

如果数据类似于以下JSON,则可以正常工作:

   {
          "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"

              }
            }
          }
   }

我认为问题出在ID上。请让我知道如何解析以下JSON中的ID "-KS9LUpZWo5UqtWKJwJ8"

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

1 个答案:

答案 0 :(得分:1)

试试这个:

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;
  }
}

实施例

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

您可以像这样迭代Map:

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