从Firebase检索子数据

时间:2016-04-28 19:36:09

标签: android firebase firebase-realtime-database

我想从我的firebase中检索数据,但是我在子项(纬度和经度)上得到null。

我不知道如何访问这些子项。我想在我的两个类(MyLatLng.class和Waypoints.class)中将jackson反序列化为java的问题

-

JSON STRUCTURE

{
   "-KGRY2El-uvfSH3LKOLp" : {
     "Waypoints" : {
       "-KGRY2El-uvfSH3LKOLq" : {
         "latitude" : 223,
         "longitude" : 31
       },
       "-KGRY2El-uvfSH3LKOLr" : {
         "latitude" : 123,
         "longitude" : 23
       }
     },
     "timeStamp" : "2016/04/27 13:00:56"
   },
   "-KGRY2OhWFJ_1Zd9RDaU" : {
     "Waypoints" : {
       "-KGRY2OhWFJ_1Zd9RDaV" : {
         "latitude" : 223,
         "longitude" : 31
  },
  "-KGRY2OquKzf1EhmLpTl" : {
    "latitude" : 123,
    "longitude" : 23
  }
 },
   "timeStamp" : "2016/04/27 13:00:57"
 }
}

Logcat,错误

java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.rasmusjosefsson.rjcar.Data.Waypoints.getLongitude()' on a null object reference
at com.example.rasmusjosefsson.rjcar.MapListActivity$1.populateViewHolder(MapListActivity.java:49)

at com.example.rasmusjosefsson.rjcar.MapListActivity$1.populateViewHolder(MapListActivity.java:41)
at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:191)

父母身份

@JsonIgnoreProperties(ignoreUnknown = true)
public class MyLatLng {

@JsonProperty("timeStamp")
private String timeStamp;

@JsonProperty("waypoints")
private Map<String, Waypoints> waypoints;

public MyLatLng(String timeStamp, HashMap<String, Waypoints> waypoints)          {
    this.timeStamp = timeStamp;
    this.waypoints = waypoints;
}

public MyLatLng() {}

public String getTimeStamp() {
    return timeStamp;
}

public Map<String, Waypoints> getWaypoints() {
    return waypoints;
}

public void setWaypoints(Map<String, Waypoints> waypoints) {
    this.waypoints = waypoints;
 }
}

儿童数据

@JsonIgnoreProperties(ignoreUnknown = true)
public class Waypoints {

@JsonProperty("longitude")
private double longitude;

@JsonProperty("latitude")
private double latitude;

public Waypoints(double latitude, double longitude) {
    this.latitude = latitude;
    this.longitude = longitude;
}

public Waypoints() {

}

public double getLongitude() {
    return longitude;
}

public double getLatitude() {
    return latitude;
}

public void setLongitude(double longitude) {
    this.longitude = longitude;
 }
}

活动

public class MapListActivity extends AppCompatActivity {

private RecyclerView mRecyclerView;
private FirebaseRecyclerAdapter<MyLatLng, LatLngViewHolder> mRecycleViewAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_list);
    Firebase.setAndroidContext(this);
    Firebase ref = new Firebase("https://shining-inferno-2821.firebaseio.com/");
    Query locRef = ref.limitToLast(10);

    mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
    mRecyclerView.setHasFixedSize(false);
    LinearLayoutManager manager = new LinearLayoutManager(this);
    manager.setReverseLayout(false);
    mRecyclerView.setLayoutManager(manager);

    mRecycleViewAdapter = new FirebaseRecyclerAdapter<MyLatLng, LatLngViewHolder>(MyLatLng.class, R.layout.list_item, LatLngViewHolder.class, locRef) {
        @Override
        protected void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLng myLatLng, int i) {

             String key = this.getRef(i).getKey();
             latLngViewHolder.locationA.setText(key);

             latLngViewHolder.locationB.setText(String.valueOf(myLatLng.getWaypoints().get(key).getLongitude()));
             latLngViewHolder.date.setText(String.valueOf(myLatLng.getTimeStamp()));
        }

    };

    mRecyclerView.setAdapter(mRecycleViewAdapter);
 }
}

更新

错误密钥的图片。 Click here

1 个答案:

答案 0 :(得分:0)

您似乎正在使用MyLatLng的密钥来尝试访问Waypoints。让我们重写一下你的代码,这样就更明显了:

protected void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLng myLatLng, int i) {
     String myLatLongKey = this.getRef(i).getKey();
     latLngViewHolder.locationA.setText(myLatLongKey);

     WayPoints myWayPoint = myLatLng.getWaypoints().get(myLatLongKey);
     latLngViewHolder.locationB.setText(String.valueOf(myWayPoint.getLongitude()));
     latLngViewHolder.date.setText(String.valueOf(myLatLng.getTimeStamp()));
}

由于没有带有该键的路径(因为它是MyLatLng的键)Map.get()返回null,并且如果您尝试在其上调用方法,则会得到空指针异常。

我不知道你的代码与路标有什么关系,但你会想要使用Map类的操作来编写代码。 E.g。

String waypointsMsg = "";
for (WayPoints wayPoint: myLatLng.getWaypoints()) {
    waypointsMsg += "("+wayPoint.getLongitude()+","+wayPoint.getLatitude()+")\n";
}
latLngViewHolder.locationB.setText(waypointsMsg);