我需要显示Firebase实时数据库中的时间戳。我尝试使用一些我看到的示例来完成此操作,但是它对我没有任何标记,并且我不知道如何下载日期以及如何格式化该日期以使其可见。这是我的代码,我想知道我应该在哪一部分工作以及如何发送它以显示在TextView中。
Array-Fedd
public class Feed {
public String tipo_feed;
public Boolean is_gratis;
public String imagen_feed;
public String costo_pdf;
public String url_tipo;
public String imagen_admin;
public String nombre_admin;
public String timestamp;
public String descripcion;
public Feed(String tipo_feed, Boolean is_gratis, String imagen_feed, String costo_pdf, String url_tipo, String imagen_admin, String nombre_admin, String timestamp, String descripcion) {
this.tipo_feed = tipo_feed;
this.is_gratis = is_gratis;
this.imagen_feed = imagen_feed;
this.costo_pdf = costo_pdf;
this.url_tipo = url_tipo;
this.imagen_admin = imagen_admin;
this.nombre_admin = nombre_admin;
this.timestamp = timestamp;
this.descripcion = descripcion;
}
public Feed(){}
public String getTipo_feed() {
return tipo_feed;
}
public void setTipo_feed(String tipo_feed) {
this.tipo_feed = tipo_feed;
}
public Boolean getIs_gratis() {
return is_gratis;
}
public void setIs_gratis(Boolean is_gratis) {
this.is_gratis = is_gratis;
}
public String getImagen_feed() {
return imagen_feed;
}
public void setImagen_feed(String imagen_feed) {
this.imagen_feed = imagen_feed;
}
public String getCosto_pdf() {
return costo_pdf;
}
public void setCosto_pdf(String costo_pdf) {
this.costo_pdf = costo_pdf;
}
public String getUrl_tipo() {
return url_tipo;
}
public void setUrl_tipo(String url_tipo) {
this.url_tipo = url_tipo;
}
public String getImagen_admin() {
return imagen_admin;
}
public void setImagen_admin(String imagen_admin) {
this.imagen_admin = imagen_admin;
}
public String getNombre_admin() {
return nombre_admin;
}
public void setNombre_admin(String nombre_admin) {
this.nombre_admin = nombre_admin;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getDescripcion() {
return descripcion;
}
public void setDescripcion(String descripcion) {
this.descripcion = descripcion;
}
}
AdapterFeed.java
public class AdapterFeed extends RecyclerView.Adapter<AdapterFeed.FeedViewHolder>
implements View.OnClickListener{
AdapterFeed adapterFeed;
private View.OnClickListener listener;
ArrayList<Feed> feeds;
URL imageURL = null;
HttpURLConnection connection = null;
static DBProvider dbProvider;
Bitmap bm = null;
private static final String TAG = "BAJARINFO:";
public static class FeedViewHolder extends RecyclerView.ViewHolder{
ImageView imgAdmin,imgFeed;
TextView txtNombreAdmin;
TextView txtDescripcion;
TextView txtPrecio;
TextView txtTimeStamp;
TextView txtTipoFeed;
TextView urlTipo;
Boolean isGratis;
public FeedViewHolder (View itemView) {
super(itemView);
imgAdmin=itemView.findViewById(R.id.imgPostPersona);
imgFeed=itemView.findViewById(R.id.imagPostU);
txtNombreAdmin=itemView.findViewById(R.id.txtNombrePost);
txtDescripcion=itemView.findViewById(R.id.txtDescripcionPost);
txtPrecio=itemView.findViewById(R.id.txtTipoPostPrecio);
txtTimeStamp=itemView.findViewById(R.id.txtTiempoPost);
txtTipoFeed=itemView.findViewById(R.id.btnPostTipo);
isGratis=false;
}
}
public AdapterFeed(ArrayList<Feed>feeds){
this.feeds=feeds;
}
@Override
public AdapterFeed.FeedViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(parent.getContext()).inflate(R.layout.usuario_14_diseno,null,false);
v.setOnClickListener(this);
AdapterFeed.FeedViewHolder holder=new AdapterFeed.FeedViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull AdapterFeed.FeedViewHolder holder, int position) {
Feed feed = feeds.get(position);
holder.txtNombreAdmin.setText(feed.getNombre_admin());
holder.txtDescripcion.setText(feed.getDescripcion());
holder.txtPrecio.setText(feed.getCosto_pdf());
holder.txtTipoFeed.setText(feed.getTipo_feed());
//In this textView I want to put the Timestamp
holder.txtTimeStamp.setText(feed.getTimestamp());
}
@Override
public int getItemCount() {
return feeds.size();
}
public void setOnClickListener(View.OnClickListener listener) {
this.listener = listener;
}
@Override
public void onClick(View v) {
if (listener!=null){
listener.onClick(v);
}
}
}
UsarioHome.java
public class UsuarioHome extends AppCompatActivity {
ImageButton imgPlan,imgPerfil,imgChat,imgHome;
static DBProvider dbProvider;
BajarInfo bajarInfo;
private static final String TAG = "BAJARINFO:";
AdapterFeed adapterFeed;
ArrayList<Feed> feeds;
RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.usuario_14_feed);
bajarFeed();
dbProvider = new DBProvider();
bajarInfo = new BajarInfo();
imgPlan=findViewById(R.id.imgPlan);
imgPerfil=findViewById(R.id.imgPerfil);
imgChat=findViewById(R.id.imgChat);
imgHome=findViewById(R.id.imgHome);
imgPlan.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(UsuarioHome.this, UsuarioPlan.class);
startActivity(intent);
}
});
imgPerfil.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(UsuarioHome.this, UsuarioPerfil.class);
startActivity(intent);
}
});
imgChat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(UsuarioHome.this, UsuarioChat.class);
startActivity(intent);
}
});
imgChat.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(UsuarioHome.this, UsuarioChat.class);
startActivity(intent);
}
});
imgHome.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(UsuarioHome.this, DetallePdf.class);
startActivity(intent);
}
});
recyclerView=findViewById(R.id.recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
feeds=new ArrayList<>();
adapterFeed=new AdapterFeed(feeds);
recyclerView.setAdapter(adapterFeed);
adapterFeed.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(feeds.get(recyclerView.getChildAdapterPosition(view)).getTipo_feed().equals(Contants.VIDEO)){
Intent intent = new Intent(UsuarioHome.this, Video.class);
startActivity(intent);
}
else if(feeds.get(recyclerView.getChildAdapterPosition(view)).getTipo_feed().equals(Contants.IMAGEN)){
Intent intent = new Intent(UsuarioHome.this, Imagen.class);
startActivity(intent);
}
else if(feeds.get(recyclerView.getChildAdapterPosition(view)).getTipo_feed().equals(Contants.PDF)){
if (feeds.get(recyclerView.getChildAdapterPosition(view)).getIs_gratis()){
Intent intent = new Intent(UsuarioHome.this, DetallePdf.class);
startActivity(intent);
}
else {
Intent intent = new Intent(UsuarioHome.this, PantallaPDF.class);
intent.putExtra("ViewType","internet");
startActivity(intent);
}
}
else {
Toast.makeText(UsuarioHome.this, "hola", Toast.LENGTH_SHORT).show();
}
}
});
}
public void bajarFeed(){
//feeds.removeAll(feeds);
dbProvider = new DBProvider();
dbProvider.tablaFeed().addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
Log.e(TAG, "Feed: " + dataSnapshot);
Feed feed = snapshot.getValue(Feed.class);
feeds.add(feed);
adapterFeed.notifyDataSetChanged();
}
}
else {
Log.e(TAG, "Feed: ");
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.e(TAG, "ERROR: ");
}
});
}
}