Android Studio将TimeStamp发布到textview中

时间:2018-03-16 15:46:22

标签: java android xml firebase

我目前正在为android工作室应用程序进行viber活动。我的应用程序能够确定谁发送了消息,但它当前无法为其添加时间戳。 singlemessage_layout.xml

    <TextView
        android:id="@+id/messageTime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="5dp"
        android:text=""
        android:textColor="#000000"
        android:textStyle="bold" />

message.java

import java.util.Date;

public class Message {
    private String content,username;
    private long time;
    public Message()
    {

    }
    public Message(String content,String username)
    {
        this.content=content;
        this.username = username;
    }

    public String getUsername() {
        return username;
    }

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

    public String getContent()
    {
      return content;
    }
    public void setContent(String content)
    {
        this.content = content;
    }

    public long getTime() {
        return time;
    }

    public void setTime(long time) {
        this.time = time;
    }
}

讨论board.java

import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.text.format.DateUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.text.format.DateFormat;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.Date;


public class discussionboard extends AppCompatActivity {
    private EditText editMessage;
    private DatabaseReference mDatabase;
    private RecyclerView mMessageList;
    private FirebaseAuth mAuth;
    private FirebaseDatabase database;
    private FirebaseAuth.AuthStateListener mAuthListener;
    private FirebaseUser mCurrentUser;
    private DatabaseReference mDatabaseUsers;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_discussionboard);
        editMessage=(EditText)findViewById(R.id.editMessageE);
        mDatabase = FirebaseDatabase.getInstance().getReference().child("Messages");
        mMessageList = (RecyclerView)findViewById(R.id.messageRec);
        mMessageList.setHasFixedSize(true);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        linearLayoutManager.setStackFromEnd(true);
        mMessageList.setLayoutManager(linearLayoutManager);
        mAuth = FirebaseAuth.getInstance();
    }
    public void sendButtonClicked(View view)
    {
        mCurrentUser = mAuth.getCurrentUser();
        mDatabaseUsers = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid());
        final String messageValue = editMessage.getText().toString().trim();
        if(!TextUtils.isEmpty(messageValue))
        {
            final DatabaseReference newPost = mDatabase.push();
            mDatabaseUsers.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    newPost.child("content").setValue(messageValue);
                    newPost.child("username").setValue(dataSnapshot.child("name").getValue()).addOnCompleteListener(new OnCompleteListener<Void>() {
                        @Override
                        public void onComplete(@NonNull Task<Void> task) {

                        }
                    });
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });
            mMessageList.scrollToPosition(mMessageList.getAdapter().getItemCount());
            editMessage.setText("");


        }

    }
    @Override
    protected void onStart()
    {
      super.onStart();
        FirebaseRecyclerAdapter<Message,MessageViewHolder> FBRA = new FirebaseRecyclerAdapter<Message, MessageViewHolder>(
                Message.class,
                R.layout.singlemessagelayout,
                MessageViewHolder.class,
                mDatabase) {
            @Override
            protected void populateViewHolder(MessageViewHolder viewHolder, Message model, int position) {
                viewHolder.setContent(model.getContent());
                viewHolder.setUsername(model.getUsername());

            }
        };
        mMessageList.setAdapter(FBRA);
    }
    public static class MessageViewHolder extends RecyclerView.ViewHolder
    {
        View mView;
        public MessageViewHolder(View itemView) {
            super(itemView);
            mView=itemView;
        }
        public void setContent(String content)
        {
            TextView messag_content = (TextView) mView.findViewById(R.id.messageText);
            messag_content.setText(content);
        }
        public void setUsername(String username)
        {
            TextView username_content = (TextView)mView.findViewById(R.id.usernametext);
            username_content.setText(username);
        }
    }

}

如何在messageTime textview中发布当前日期

1 个答案:

答案 0 :(得分:0)

这样做的简单方法就是这样

获取当前TimeStamp

Long timeStamp = System.currentTimeMillis()/1000;
String currentTimeStamp = timeStamp.toString();

然后将其设置为textView,就像这样

textView.setText(currentTimeStamp); //this will return the textView with the current TimeStamp

编辑:如果将来需要这个timeStamp,你应该将它上传到你的firebase数据库,只需使用这行存储时间戳并为它创建一个新的setValue

示例:在您的sendButtonClicked中,您应该添加此内容以获取timeStamp并将其与消息一起发送

    Long timeStamp = System.currentTimeMillis()/1000;
    String currentTimeStamp = timeStamp.toString();
    newPost.child("username").child("timeStamp").setValue(currentTimeStamp);

希望有所帮助

快乐的编码!