Firebase检索

时间:2016-02-18 05:26:30

标签: java android firebase

这是Firebase数据: [Firebase数据] [1]

我必须从员工处检索所有字段并将其存储在数组中 现在数据更改toast消息即将到来,但它已经很晚了。

Firebase.setAndroidContext(this);
            rootRef = new Firebase(
                    "https://mylink.firebaseio.com/user/employee");
            Toast.makeText(getApplicationContext(), "2",
                    Toast.LENGTH_SHORT).show();


            rootRef.addValueEventListener(new ValueEventListener() {

                public void onCancelled(FirebaseError arg0) {
                    // TODO Auto-generated method stub
                     tv1.setText("The read failed");
                        Toast.makeText(getApplicationContext(), "failed",
                                Toast.LENGTH_SHORT).show();

                      System.out.println("The read failed: " + arg0.getMessage());
                }

                public void onDataChange(DataSnapshot arg0) {
                    // TODO Auto-generated method stub
                    Toast.makeText(getApplicationContext(), "data change",
                            Toast.LENGTH_SHORT).show();

                  System.out.println("There are " + arg0.getChildrenCount() + " blog posts");
                  for (DataSnapshot postSnapshot: arg0.getChildren()) {
                      Toast.makeText(getApplicationContext(), "call blogpost",
                                Toast.LENGTH_SHORT).show();

                    BlogPost post = postSnapshot.getValue(BlogPost.class);
                    Toast.makeText(getApplicationContext(), "getting value",
                            Toast.LENGTH_SHORT).show();

                    System.out.println(post.getPath() + " - " + post.getSeqno());
                    tv1.setText(post.getPath() + " - " + post.getSeqno());
                  }
                }


            });

BlogPost

public class BlogPost {


      private String path;
      private String dt;
      private String latitude;
      private String longitude;
      private String seqno;

      public BlogPost() {
            // empty default constructor, necessary for Firebase to be able to deserialize blog posts
          }
    public String getDateTime() {
        return dt;
    }
    public void setDateTime(String dt) {
        this.dt = dt;
    }
    public String getLatitude() {
        return latitude;
    }
    public void setLatitude(String latitude) {
        this.latitude = latitude;
    }
    public String getLongitude() {
        return longitude;
    }
    public void setDob(String longitude) {
        this.longitude = longitude;
    }
    public String getPath() {
        return path;
    }
    public void setPath(String path) {
        this.path = path;
    }
    public String getSeqno() {
        return seqno;
    }
    public void setSeqno(String seqno) {
        this.seqno = seqno;
    }
    }

任何人都可以帮我解决这个问题吗? 提前谢谢你。

JSON:

myfirebaselink
 user
  employee
    -KAZY5627P4MjLBa3Ae-
      DateTime: "2016-02-15 16:18:20"
      Latitude: "11.0318354"
      Longitude: "11.0318354"
      Path: "/storage/emulated/0/DCIM/100ANDRO/DSC_0070.JPG"
      Seqno: "1"
   -KAZZcBscTCAon8ShoXa
      DateTime: "2016-02-15 16:25:02"
      Latitude: "11.0318501"
      Longitude: "11.0318501"
      Path: "/storage/emulated/0/DCIM/100ANDRO/DSC_0071.JPG"
      Seqno: "2"

1 个答案:

答案 0 :(得分:2)

我假设您尝试将检索到的数据从firebase存储到数组中,假设您的URL正确且数据更改可见。我想这就是你必须要开始的方式:

Firebase julieRef = new Firebase("Your_URL");
julieRef.addValueEventListener(new ValueEventListener() {
   @Override
   public void onDataChange(DataSnapshot snapshot) {
       GenericTypeIndicator<List<String>> t = new GenericTypeIndicator<List<String>>() {};
       List<String> messages = snapshot.getValue(t);
       if( messages === null ) {
          System.out.println('No messages');
       }
       else {
          System.out.println("The first message is: " + messages.get(0) );
       }
   }

   // onCancelled...
});

希望这有帮助。