Android Firebase获取ServerValue.TIMESTAMP

时间:2018-01-30 05:20:36

标签: android firebase firebase-realtime-database

我想问一下如何使用viewholder获取ServerValue.TIMESTAMP并将其转换为显示为textView。 这是我的数据库中的代码"评级"

ratingDatabase = FirebaseDatabase.getInstance().getReference().child("Rating").push();
            Map userMap = new HashMap();
            userMap.put("comment", comments);
            userMap.put("foodId", foodId);
            userMap.put("rateValue", String.valueOf(value));
            userMap.put("userName", firebaseAuth.getCurrentUser().getEmail());
            userMap.put("timestamp", ServerValue.TIMESTAMP);

            ratingDatabase.setValue(userMap);

我试图获得评级的任何数据,但它总是为空。我怎样才能进入"评级"目录用"按键"?我只需要database.child("评级")或database.child("评级")。推?我知道push键会生成一个生成的密钥,我只想让孩子进入目录。

这是获取ServerValue.TIMESTAMP

的代码
 if (getIntent()!=null)
            {
                foodId = getIntent().getStringExtra(Common.INTENT_SHOW_COMMENT);
                if (!foodId.isEmpty() &&  foodId !=null)
                {
                    Query query = ratingTbl.orderByChild("foodId").equalTo(foodId);
                    FirebaseRecyclerOptions<Rating> options = new FirebaseRecyclerOptions.Builder<Rating>().setQuery
                            (query, Rating.class).build();

                    adapter = new FirebaseRecyclerAdapter<Rating, ShowCommentViewholder>(options) {
                        @Override
                        protected void onBindViewHolder(@NonNull final ShowCommentViewholder holder, int position, @NonNull Rating model) {
                            //String date = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(Calendar.getInstance().getTime());
                            holder.Ratingbar.setRating(Float.parseFloat(model.getRateValue()));
                            holder.TxtEmail.setText(model.getUserName());
                            holder.TxtComment.setText(model.getComment());
                           /* ratingDatabase = FirebaseDatabase.getInstance().getReference().child("Rating").addValueEventListener(new ValueEventListener() {
                                @Override
                                public void onDataChange(DataSnapshot dataSnapshot) {
                                    String time = dataSnapshot.child("comment").getValue().toString();
                                    GetTime getTimeAgo = new GetTime();
                                    long lastime = Long.parseLong(time);
                                    String LastComment= getTimeAgo.getTimeAgo(lastime, ViewComment.this);
                                    holder.Date.setText(time);
                                }

                                @Override
                                public void onCancelled(DatabaseError databaseError) {

                                }
                            });*/

                        }

这是我的评级模型类

public class Rating {
private String userName, foodId, rateValue, comment;
private long timestamp;


public Rating() {
}

public Rating(String userName, String foodId, String rateValue, String comment) {
    this.userName = userName;
    this.foodId = foodId;
    this.rateValue = rateValue;
    this.comment = comment;
    this.timestamp = timestamp;

}

public long getTime() {
    return timestamp;
}

public void setTime(long time) {
    this.timestamp = time;
}

public String getUserName() {
    return userName;
}

public void setUserName(String userName) {
    this.userName = userName;
}

public String getFoodId() {
    return foodId;
}

public void setFoodId(String foodId) {
    this.foodId = foodId;
}

public String getRateValue() {
    return rateValue;
}

public void setRateValue(String rateValue) {
    this.rateValue = rateValue;
}

public String getComment() {
    return comment;
}

public void setComment(String comment) {
    this.comment = comment;
}


}

和我的ViewHolder

public ShowCommentViewholder(View itemView) {
    super(itemView);
    TxtComment = (TextView)itemView.findViewById(R.id.ShowComment);
    TxtEmail = (TextView)itemView.findViewById(R.id.useremail);
    Ratingbar =  (RatingBar) itemView.findViewById(R.id.RatingResult);
    Date = (TextView)itemView.findViewById(R.id.date);


  }
}

希望有人可以帮助我。谢谢!

1 个答案:

答案 0 :(得分:0)

  使用viewholder将ServerValue.TIMESTAMP转换为显示为textView。

ServerValue.TIMESTAMP是自Unix纪元以来的时间,以毫秒为单位由Firebase服务器确定。要转换为日期,您需要使用 -

SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
String date = sfd.format(new Date(timestamp)) //timestamp is model.getTime() in your case
  

我怎样才能进入&#34;评级&#34;目录用&#34;按键&#34;?

否。为了只访问数据,浏览.child()push()用于向具有唯一键的节点插入数据。