2017-06-26 47 views
0

我成功了某種方式如何從Firebase數據庫檢索時間戳,但現在它顯示錯誤的時間戳。它顯示的是這樣的:01-01-1970 05:30。令人驚訝的是,該值顯示在所有帖子中。時間戳甚至不改變。從firebase數據庫檢索錯誤的時間戳

博客觀點持有人:

public static class BlogViewHolder extends RecyclerView.ViewHolder { 
      View mview; 


      public BlogViewHolder(View itemView) { 
       super(itemView); 
       mview = itemView; 
      } 

      public void setTitle(String title) { 
       TextView post_title = (TextView) mview.findViewById(R.id.blog_title); 
       post_title.setText(title); 

      } 

      public void setDesp(String desp) { 
       TextView post_desp = (TextView) mview.findViewById(R.id.blog_desp); 
       post_desp.setText(desp); 
      } 

      public void setTimestampCreated(long timestamp) { 
       TextView show_ts = (TextView) mview.findViewById(blog_timestamp); 
       SimpleDateFormat sfd=new SimpleDateFormat("dd-MM-yyyy HH:mm"); 


       Date date=new Date(timestamp); 
       show_ts.setText(sfd.format(date)); 
      } 


FirebaseRecyclerAdapter<Blog, BlogViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Blog, BlogViewHolder>(
        Blog.class, 
        R.layout.blog_row, 
        BlogViewHolder.class, mDatabase 

      ) { 
       @Override 
       protected void populateViewHolder(final BlogViewHolder viewHolder,final Blog model, int position) { 
        viewHolder.setTitle(model.getTitle()); 
        viewHolder.setDesp(model.getDesp()); 
        viewHolder.setTimestampCreated(model.getTimestampCreated()); 
       } 
      }; 

Blog.java

public class Blog { 

    String title; 
    String desp; 
long timestamp; 

public Blog() { 
    } 

    public Blog(String title, String desp, long timestamp) { 
     this.title = title; 
     this.desp = desp; 
     this.timestamp=timestamp; 
} 

public String getDesp() { 
     return desp; 
    } 

    public void setDesp(String desp) { 
     this.desp = desp; 
    } 

    public String getTitle() { 
     return title; 
    } 

    public void setTitle(String title) { 
     this.title = title; 
    } 

    public long getTimestampCreated() { 


      return timestamp; 


    } 

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

數據庫參考:

public void startPosting() { 


     final DatabaseReference newPost = databaseReference.push(); 
String title_val = title.getText().toString().trim(); 
       String desp_val = desp.getText().toString().trim(); 

newPost.child("title").setValue(title_val); 
        newPost.child("desp").setValue(desp_val); 
newPost.child("timestamp").setValue(ServerValue.TIMESTAMP); 
} 
+0

傳入'setTimestampCreated()'的'timestamp'爲零。將代碼發佈到您閱讀帖子的位置並調用'setTimestampCreated()'。 –

+0

@BobSnyder我添加了上面的代碼。 –

+0

請更新您的帖子,以顯示博客的整個班級,而不僅僅是獲取者/設置者。 –

回答

0

問題是timestamp的getter/setter名稱不遵循命名約定並且無法識別。如果您在logcat的輸出仔細看,你會看到這樣的警告消息:

W/ClassMapper: No setter/field for timestamp found on class com.xxx.Blog 

的簡單的解決辦法可能是重命名的getter/setter方法:

public long getTimestamp() { 
    return timestamp; 
} 

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

還有其他的解決方案,其中之一是@PropertyName註釋,但我認爲修復getter/setter名稱可能是最簡單的。

+0

非常感謝。它像魔術一樣工作:-) –

0

若要從火力地堡數據庫獲得TIMESTAMP,我recomand你下面的方法:

public static String getTimeDate(long timeStamp){ 
    try{ 
     DateFormat dateFormat = getDateTimeInstance(); 
     Date netDate = (new Date(timeStamp)); 
     return dateFormat.format(netDate); 
    } catch(Exception e) { 
     return "date"; 
    } 
} 

希望它有幫助。

+0

謝謝你的回覆。問題依然存在。 –