Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/firebase/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:在recyclerview中未生成人类可读的时间戳_Android_Firebase_Firebase Realtime Database - Fatal编程技术网

Android:在recyclerview中未生成人类可读的时间戳

Android:在recyclerview中未生成人类可读的时间戳,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,我试图从firebase数据库中检索时间戳,并试图在recyclerview内的textview中显示它。生成的时间戳不是人类可读的格式。它显示如下:{timestamp={.sv=timestamp} 接受者和接受者: private DatabaseReference mDatabase; HashMap<String,Object> timestampCreated; public Blog(String title,String desp,HashM

我试图从firebase数据库中检索时间戳,并试图在recyclerview内的textview中显示它。生成的时间戳不是人类可读的格式。它显示如下:{timestamp={.sv=timestamp}

接受者和接受者:

    private DatabaseReference mDatabase;
    HashMap<String,Object> timestampCreated;
     public Blog(String title,String desp,HashMap<String,Object>timestampCreated) {
            this.title = title;
            this.desp=desp;
            //this.timestam=ServerValue.TIMESTAMP;
            HashMap<String,Object> ts=new HashMap<>();
            ts.put("timestamp", ServerValue.TIMESTAMP);
    }

     public HashMap<String,Object>getTimestampCreated(){

           if(timestampCreated!=null)
           return timestampCreated;

           HashMap<String,Object> timestampCreatedObj=new HashMap<String,Object>();
           timestampCreatedObj.put("timestamp",ServerValue.TIMESTAMP);
           return timestampCreatedObj;
        }

       public void setTimestampCreated(HashMap<String,Object> timestamp){
            this.timestampCreated=timestamp;
        }

        @Exclude
        public long getTimestampCreatedLong(){
            long value=(long)timestampCreated.get("timestamp");
            convertTime(value);
            return value;
        }

     public String convertTime(Long unixtime){
            Date dateObject=new Date(unixtime);
            SimpleDateFormat dateFormat=new SimpleDateFormat("dd-mm-yyyy  hh:mm:ss");
            return dateFormat.format(dateObject);
        }

     @Override
        protected void onCreate(Bundle SavedInstanceState) {
            super.onCreate(SavedInstanceState);
     mDatabase = FirebaseDatabase.getInstance().getReference().child("Blog");
            mDatabase.keepSynced(true);

ViewHolder:



    public void setTimestampCreated(HashMap<String, Object> timestamp) {
               TextView show_ts = (TextView) mview.findViewById(blog_timestamp);
              show_ts.setText(String.valueOf(timestamp));


           }

Adapter:



    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.setTimestampCreated(model.getTimestampCreated());


                    }
                };
                mbloglist.setAdapter(firebaseRecyclerAdapter);
私有数据库参考mDatabase;
创建HashMap;
公共博客(字符串标题、字符串desp、HashMaptimestampCreated){
this.title=标题;
这个.desp=desp;
//this.timestam=ServerValue.TIMESTAMP;
HashMap ts=新的HashMap();
ts.put(“timestamp”,ServerValue.timestamp);
}
公共HashMapgetTimestampCreated(){
if(timestampCreated!=null)
创建返回时间戳;
HashMap timestampCreatedObj=新HashMap();
timestampCreatedObj.put(“timestamp”,ServerValue.timestamp);
返回时间戳;
}
已创建公共void settimestamp(HashMap timestamp){
this.timestampCreated=时间戳;
}
@排除
公共长getTimestampCreatedLong(){
long value=(long)timestampCreated.get(“timestamp”);
转换时间(值);
返回值;
}
公共字符串转换时间(长unixtime){
日期对象=新日期(unixtime);
SimpleDataFormat dateFormat=新的SimpleDataFormat(“dd-mm-yyy-hh:mm:ss”);
返回dateFormat.format(dateObject);
}
@凌驾
创建时受保护的void(Bundle SavedInstanceState){
super.onCreate(SavedInstanceState);
mDatabase=FirebaseDatabase.getInstance().getReference().child(“Blog”);
mDatabase.keepSynced(true);
持证人:
已创建公共void settimestamp(HashMap timestamp){
TextView show_ts=(TextView)mview.findViewById(blog_时间戳);
show_ts.setText(String.valueOf(timestamp));
}
适配器:
FirebaseRecyclerAdapter FirebaseRecyclerAdapter=新的FirebaseRecyclerAdapter(
Blog.class,
R.layout.blog_行,
BlogViewHolder.class,mDatabase
) {
@凌驾
受保护的void populateViewHolder(最终BlogViewHolder,最终Blog模型,int位置){
setTimestampCreated(model.getTimestampCreated());
}
};
mbloglist.setAdapter(firebaseRecyclerAdapter);
数据库图像:

在Android软件包中使用util类

String humanReadableTime = DateFormat.format("dd MMM yyyy, HH:mm", timestamp);

您正在以正确的方式将数据设置为
时间戳
。请记住,当您设置
时间戳
时,您将其设置为
映射
,但当您检索它时,您将其检索为
。因此,为了正确设置,我建议您使用以下方法:

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

希望它有帮助。

我们必须在代码中的某个地方调用它还是定义它就足够了?你可以在你需要的类/活动中定义这个方法,也可以在一个单独的类,一个助手类中定义它。你只需要使用从数据库中得到的
Long
值作为参数来调用它。我在gettimest中调用了它ampcreatedlong()使用getTimeDate(value);问题仍然存在。是否确定您从正确的位置获取了值,并且没有缺少一个孩子?能否向我们显示您的数据库结构?可以,但我在代码中看不到您查询数据库的位置。我看不到
DatabaseReference