gpt4 book ai didi

android - 以人类可读的形式在 FireBaseRecyclerView 适配器中填充日期和时间

转载 作者:行者123 更新时间:2023-11-29 19:21:12 25 4
gpt4 key购买 nike

我正在开发一个博客应用程序,我在其中创建了 card_view,其中显示了用户名、个人资料图片、发布时间和其他详细信息。到目前为止,一切都很好,除了一件事时间和日期。现在这是一件需要花费大量时间研究的事情,但问题是我的工作没有得到解决。

<强>1。根据时间和日期工作到现在

  • 我所做的是在 TIMESTAMP 中保存了用户发帖时间。
  • 现在,为了实现我的目标,即以人类可读的形式检索 post_time,我对如何将数据从时间戳形式更改为非日和年、小时格式进行了大量研究。

我的 POJO 中的 post_time 很长,它已成功存储在我的数据库中

在我的 BlogFragment 中,我正在检索我的详细信息。

<强>2。博客.java

public void setPost_time(Long post_time){
TextView time = (TextView) mView.findViewById(R.id.post_time);
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = null;
try {
date = dateFormat.parse(post_time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
time.setText(dateFormat.format(date));
}

我在遵循 Stackoverflow 上的大量代码后完成了上述代码,但从未得到解决方案。

我搜索的解决方案:

To fix nullpointerexception on date format

Java Illegal argument on the date format

Firebase.TIMESTAMP Docs

Working with date class

Populate the date and time in human readable form in listview

为了将 TIMESTAMP 格式转换为人类可读的格式,我又尝试了一种方法,如下所示:

Convert timestamp format into hour and date format

当我在 Blog.java 中实现上述代码时,我的 logcat 中出现错误

<强>3。 LogCat

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aadarshkumarsingh.project1, PID: 10460
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.lang.String to long
at com.google.android.gms.internal.zzbqi.zzaA(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzc(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi$zza.zze(Unknown Source)
at com.google.android.gms.internal.zzbqi$zza.zzaG(Unknown Source)
at com.google.android.gms.internal.zzbqi.zze(Unknown Source)
at com.google.android.gms.internal.zzbqi.zzb(Unknown Source)
at com.google.android.gms.internal.zzbqi.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:151)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:140)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:183)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5825)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5094)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4970)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2029)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1414)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1377)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:578)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3315)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3124)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1549)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:305)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:871)
at android.view.Choreographer.doCallbacks(Choreographer.java:683)
at android.view.Choreographer.doFrame(Choreographer.java:616)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:857)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

W/DynamiteModule:找不到 com.google.firebase.auth 的本地模块描述符类。

我的数据库是这样的: Firebase App Database

数据库快照

mDatabaseUsers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//now getting the title,desc and image inside the specified field in our
//databse
newPost.child("title").setValue(title_val);
newPost.child("desc").setValue(title_desc);
newPost.child("image").setValue(downloadUrl.toString());
//for future purpose if we want the app to have a feature of
//visiting the user's profile
newPost.child("uid").setValue(mCurrentUser.getUid());
newPost.child("post_time").setValue(ServerValue.TIMESTAMP);
//returns the data from the users database object
newPost.child("username").setValue(dataSnapshot.child("name").getValue());
newPost.child("userpic").setValue(dataSnapshot.child("image").getValue());

}

请帮我解决这个问题,因为这对我来说非常重要,而且会让我的应用看起来更加用户友好。

更新

@Override
public void onStart() {
super.onStart();

FirebaseRecyclerAdapter<NewsBlog, BlogViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<NewsBlog, BlogViewHolder>(
NewsBlog.class,
R.layout.blog_row,
BlogViewHolder.class,
mChildRef) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, NewsBlog model, int position) {

viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getContext(),model.getImage());
viewHolder.setDesc(model.getDesc());
viewHolder.setUsername(model.getUsername());
viewHolder.setPost_time(model.getPost_time());
viewHolder.setUserpic(getContext(),model.getUserpic());
}
};

mBlogList.setAdapter(firebaseRecyclerAdapter);
}

更新

NewsBlog.java

public class NewsBlog {

String image;
String desc;
String title;
String username;
Long post_time;
String userpic;


public NewsBlog(){

}

public NewsBlog(String image, String desc, String title, String username,
String userpic, Long post_time) {
this.image = image;
this.desc = desc;
this.title = title;
this.post_time = post_time;

this.username = username;
this.userpic = userpic;

}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public String getDesc() {
return desc;
}

public void setDesc(String desc) {
this.desc = desc;
}

public String getTitle() {
return title;
}

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

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public Long getPost_time() {
return post_time;
}

public void setPost_time(Long post_time) {
this.post_time = post_time;
}

public String getUserpic() {
return userpic;
}

public void setUserpic(String userpic) {
this.userpic = userpic;
}}

Blog.java

public class Blog extends Fragment {

private RecyclerView mBlogList;

private DatabaseReference mDatabse,mChildRef;

private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;

private DatabaseReference mDatabaseUsers,mChildUsers;

public Blog() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_blog, container, false);

mDatabse = FirebaseDatabase.getInstance().getReference();
mChildRef = mDatabse.child("Blog");
mChildRef.keepSynced(true);

mDatabaseUsers = FirebaseDatabase.getInstance().getReference();
mChildUsers = mDatabaseUsers.child("Users");
mChildUsers.keepSynced(true);

mBlogList = (RecyclerView) view.findViewById(R.id.blog_list);

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);

mBlogList.setHasFixedSize(true);
mBlogList.setLayoutManager(linearLayoutManager);

return view;
}

@Override
public void onStart() {
super.onStart();

FirebaseRecyclerAdapter<NewsBlog, BlogViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<NewsBlog, BlogViewHolder>(
NewsBlog.class,
R.layout.blog_row,
BlogViewHolder.class,
mChildRef) {
@Override
protected void populateViewHolder(BlogViewHolder viewHolder, NewsBlog model, int position) {

viewHolder.setTitle(model.getTitle());
viewHolder.setImage(getContext(),model.getImage());
viewHolder.setDesc(model.getDesc());
viewHolder.setUsername(model.getUsername());
viewHolder.setPost_time(model.getPost_time());
viewHolder.setUserpic(getContext(),model.getUserpic());
}
};

mBlogList.setAdapter(firebaseRecyclerAdapter);
}

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.post_title);
post_title.setText(title);
}

public void setImage(Context ctx, String image){
ImageView post_image = (ImageView) mView.findViewById(R.id.post_image);
Picasso.with(ctx).load(image).into(post_image);
}

public void setDesc(String desc){
TextView post_desc = (TextView) mView.findViewById(R.id.post_desc);
post_desc.setText(desc);
}

public void setUsername(String username){
TextView mUsername = (TextView) mView.findViewById(R.id.post_username);
mUsername.setText(username);
}

public void setPost_time(Long post_time){
TextView time = (TextView) mView.findViewById(R.id.post_time);
DateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");
Date date = null;
try {
date = dateFormat.parse(post_time.toString());
} catch (ParseException e) {
e.printStackTrace();
}
time.setText(dateFormat.format(date));
}

public void setUserpic(Context ctx, String userpic){
ImageView userImage = (ImageView) mView.findViewById(R.id.post_userpic);
Picasso.with(ctx).load(userpic).into(userImage);
}
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setHasOptionsMenu(true);
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.blog_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

if(id == R.id.blog_items1){
//for checking whether the user has logged in or not
mAuth = FirebaseAuth.getInstance();

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() == null){
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Login");
Login login = new Login();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, login);
fragmentTransaction.commit();

} else {
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("AddBlog");
PostActivity postActivity = new PostActivity();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, postActivity);
fragmentTransaction.commit();
}
}
};

mAuth.addAuthStateListener(mAuthListener);

mAuth.removeAuthStateListener(mAuthListener);

}else if(id == R.id.logout) {
//logout action
mAuth = FirebaseAuth.getInstance();

mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser() != null){
mAuth.signOut();
Toast.makeText(getContext()," Successfully Logged Out!",Toast.LENGTH_SHORT)
.show();
((AppCompatActivity)getActivity()).getSupportActionBar().setTitle("Login");
Login login = new Login();
android.support.v4.app.FragmentTransaction fragmentTransaction =
getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, login);
fragmentTransaction.commit();

}else{
Toast.makeText(getContext(),"Log in first!",Toast.LENGTH_SHORT)
.show();
}
}
};
mAuth.addAuthStateListener(mAuthListener);


mAuth.removeAuthStateListener(mAuthListener);
}

return super.onOptionsItemSelected(item);
}}

最佳答案

如果这有效,请告诉我。

public void setPost_time(Long post_time){

TextView time = (TextView) mView.findViewById(R.id.post_time);
SimpleDateFormat sfd = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss");

try {
time.setText(sfd.format(new Date(post_time)));
} catch (ParseException e) {
e.printStackTrace();
}

}

关于android - 以人类可读的形式在 FireBaseRecyclerView 适配器中填充日期和时间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42425915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com