如何为CardView添加时间戳

时间:2017-02-17 20:46:14

标签: date android-recyclerview timestamp fragment android-cardview

我正在使用FirebaseRecyclerAdapter我的问题或问题是我需要显示我的帖子卡的时间戳,所以我搜索了3个月,我无法解决这个问题所以请兄弟我已经尽了最大努力,是时候了其他人的帮助请记住,我对时间戳不知道......

这是我想要展示的一个例子:3小时前,1天,刚才。

如果有人通过添加解决我的JAVA片段来解决这个问题,我会为他感到自豪。

这是我的片段

公共类挑战扩展了片段{

private RecyclerView mPostList;
private DatabaseReference mDatabase,mDatabaseFriends,mDatabaseLike;
private SwipeRefreshLayout mRefreshLayout;
private boolean mProcessLike = false;
private String AA;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mListener;
private Query mQuery;
private FirebaseUser mCurrentUser;
public Challenges() {}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    MultiDex.install(getActivity());}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v= inflater.inflate(R.layout.challenges, container, false);
    mListener = new FirebaseAuth.AuthStateListener() {
        @Override
        public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
            final FirebaseUser User = firebaseAuth.getCurrentUser();
            if (User == null) {
                Intent A = new Intent(getActivity(), Login.class);
                startActivity(A);}}};

    mPostList=(RecyclerView)v.findViewById(R.id.PostList);
    mPostList.setHasFixedSize(true);
    mPostList.setItemAnimator(new DefaultItemAnimator());
    mPostList.setLayoutManager(new LinearLayoutManager(getActivity()));

    //Firebase
    mDatabase= FirebaseDatabase.getInstance().getReference().child("Posts");
    mDatabaseFriends= FirebaseDatabase.getInstance().getReference().child("Friends");
    mDatabaseLike = FirebaseDatabase.getInstance().getReference().child("Likes");
    mDatabase.keepSynced(true);
    mDatabaseLike.keepSynced(true);
    mAuth = FirebaseAuth.getInstance();
    mCurrentUser = mAuth.getCurrentUser();
    mRefreshLayout=(SwipeRefreshLayout)v.findViewById(R.id.RefreshLayout);
    //mQuery = mDatabase.orderByChild("UID").equalTo(String.valueOf(mDatabaseFriends.child(mCurrentUser.getUid()).child("Accepted")));
    mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            onStart();}});

    return v;}

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

    mAuth.addAuthStateListener(mListener);
    mRefreshLayout.setRefreshing(true);
    final FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Getting_Posts, PostViewHolder>(
            Getting_Posts.class, R.layout.post_card_design, PostViewHolder.class, mDatabase) {
        @SuppressLint("SetTextI18n")
        @Override
        protected void populateViewHolder(final PostViewHolder viewHolder, final Getting_Posts model, int position) {

            final String Post_Key = getRef(position).getKey();
            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent A = new Intent(getActivity(), com.pcsoftgroup.test.activities.Single_view.class);
                    A.putExtra("Key", Post_Key);
                    startActivity(A);}
            });

            viewHolder.setUsername(model.getUsername());
            viewHolder.setProfile(getActivity().getApplicationContext(), model.getProfile());
            viewHolder.setDescribe(model.getDescribe());
            viewHolder.setWallpaper(getActivity().getApplicationContext(), model.getWallpaper());   
            viewHolder.setLike(Post_Key);
            viewHolder.setLikeNum(Post_Key);
            viewHolder.setCommentNum(Post_Key);
            viewHolder.mPostTime.setText(model.getTime());

            viewHolder.mLike.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mProcessLike = true;
                    mDatabaseLike.addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            if (mProcessLike) {
                                if (dataSnapshot.child(Post_Key).hasChild(mCurrentUser.getUid())) {
                                    mDatabaseLike.child(Post_Key).child(mCurrentUser.getUid()).removeValue();
                                    mProcessLike = false;
                                } else {
                                    mDatabaseLike.child(Post_Key).child(mCurrentUser.getUid()).setValue("LIKED");
                                    mProcessLike = false;
                                }
                            }
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {
                        }
                    });
                }
            });

            viewHolder.mShare.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent sendIntent = new Intent();
                    sendIntent.setAction(Intent.ACTION_SEND);
                    //sendIntent.putExtra(Intent.EXTRA_TEXT,viewHolder.mComment.getText().toString());
                    sendIntent.setType("text/plain");
                    Intent.createChooser(sendIntent, "Share via");
                    startActivity(sendIntent);
                }
            });

            mRefreshLayout.setRefreshing(false);
            mRefreshLayout.setEnabled(true);
        }
    };

    mPostList.setAdapter(firebaseRecyclerAdapter);}

public static class PostViewHolder extends RecyclerView.ViewHolder{
    //Main
    View mView;
    ImageButton mLike;
    ImageButton mCommentBtn;
    ImageButton mShare;
    //Firebase
    FirebaseAuth mAuth;
    FirebaseUser mCurrentUser;
    DatabaseReference mDatabaseLike;
    DatabaseReference mDatabaseComment;
    public PostViewHolder(View itemView) {
        super(itemView);
        //Main
        mView=itemView;            
        mLike=(ImageButton) mView.findViewById(R.id.PostLike);
        mCommentBtn = (ImageButton) mView.findViewById(R.id.PostCommentBtn);
        mShare = (ImageButton) mView.findViewById(R.id.PostShare);
        //Firebase
        mAuth =FirebaseAuth.getInstance();
        mCurrentUser=mAuth.getCurrentUser();
        mDatabaseComment = FirebaseDatabase.getInstance().getReference().child("Comments");
        mDatabaseLike= FirebaseDatabase.getInstance().getReference().child("Likes");
        mDatabaseLike.keepSynced(true);
        mDatabaseComment.keepSynced(true);
       }

    //Like
    void setLike(final String Post_Key){
        mDatabaseLike.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                if (dataSnapshot.child(Post_Key).hasChild(mCurrentUser.getUid())){
                    mLike.setImageResource(R.drawable.like_icon);
                }else {mLike.setImageResource(R.drawable.unlike_icon);}}

            @Override
            public void onCancelled(DatabaseError databaseError) {}});}

    void setLikeNum(final String Post_Key){
        mDatabaseLike.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String A = String.valueOf(dataSnapshot.child(Post_Key).getChildrenCount());
                TextView PostLikeNum = (TextView) mView.findViewById(R.id.PostLikeNum);
                PostLikeNum.setText(A);}

            @Override
            public void onCancelled(DatabaseError databaseError) {}});}

    void setCommentNum(final String Post_Key){
        mDatabaseComment.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                String C = String.valueOf(dataSnapshot.child(Post_Key).getChildrenCount());
                TextView PostCommentNum = (TextView) mView.findViewById(R.id.PostCommentNum);
                PostCommentNum.setText(C);}

            @Override
            public void onCancelled(DatabaseError databaseError) {}});}

    //Username
    public void setUsername(String Username){
        TextView PostUsername = (TextView) mView.findViewById(R.id.PostUsername);
        PostUsername.setText(Username);}

   //Profile
    void setProfile(Context ctx2, String Profile){
        ImageView PostProfile = (ImageView) mView.findViewById(R.id.PostUserImage);
        if (Profile == null){PostProfile.setVisibility(View.VISIBLE);}
        else {PostProfile.setVisibility(View.VISIBLE);
            Picasso.with(ctx2).load(Profile).resize(120,120).into(PostProfile);}}

    //Describe
    void setDescribe(String Describe){
        TextView PostDescribe = (TextView) mView.findViewById(R.id.PostDesc);
        PostDescribe.setText(Describe);}


    //Image Load
    void setWallpaper(Context ctx, String Image){
        ImageView PostWallpaper = (ImageView) mView.findViewById(R.id.PostWallpaper);
        if (Image == null){PostWallpaper.setVisibility(View.VISIBLE);}
        else {
            PostWallpaper.setVisibility(View.VISIBLE);
        Picasso.with(ctx).load(Image).resize(1200,600).into(PostWallpaper);}}
}}

这是我的getter和setter java:

public class Getting_Posts {

    private String Username;
    private String Profile;
    private String Describe;
    private String Wallpaper;

    public Getting_Posts() {
    }

    public Getting_Posts(String Username, String Profile, String Describe, String Wallpaper) {

        this.Username = Username;
        this.Profile = Profile;
        this.Describe = Describe;
        this.Wallpaper = Wallpaper;

    }

    public String getDescribe() {
        return Describe;
    }

    public void setDescribe(String describe) {
        Describe = describe;
    }

    public String getWallpaper() {
        return Wallpaper;
    }

    public void setWallpaper(String wallpaper) {
        Wallpaper = wallpaper;
    }

    public String getUsername() {
        return Username;
    }

    public void setUsername(String username) {
        Username = username;
    }

    public String getProfile() {
        return Profile;
    }

    public void setProfile(String profile) {
        Profile = profile;
    }
}

0 个答案:

没有答案