I am trying to get this Realtime Database to display my "texts" in chronological order. it constantly adds my "texts" into alphabetical order from top to bottom. I'm sure I need to add timestamps to fix it, I'm not 100% sure though.
Here is my main activity:
> package com.example.osada;
>
> import android.os.Bundle;
> import android.view.View;
> import android.widget.EditText;
> import android.widget.TextView;
>
> import androidx.appcompat.app.AppCompatActivity;
>
> 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.text.SimpleDateFormat;
> import java.util.Date;
>
> public class MainActivity extends AppCompatActivity {
>
> private DatabaseReference myDatabase;
> SimpleDateFormat sdf = new SimpleDateFormat("yyyy.MM.dd G 'at' HH:mm:ss z");
> String currenttime = sdf.format(new Date());
>
> @Override
> protected void onCreate(Bundle savedInstanceState) {
> super.onCreate(savedInstanceState);
> setContentView(R.layout.activity_main);
>
> myDatabase = FirebaseDatabase.getInstance() .getReference("message1");
>
> TextView textmessage = findViewById(R.id.text);
>
> myDatabase.addValueEventListener(new ValueEventListener() {
> @Override
> public void onDataChange(DataSnapshot snapshot) {
>
>
> textmessage.setText(snapshot.getValue().toString());
>
>
>
> }
>
> @Override
> public void onCancelled(DatabaseError error) {
> textmessage.setText("Error, please try again later.");
> }
> });
> }
> public void sendMessage (View view){
>
> EditText myEditText = findViewById(R.id.editTextTextPersonName);
> myDatabase.push().child("Jon").setValue(myEditText.getText().toString());
> myEditText.setText("");
> System.out.println();
>
>
>
>
> }
> }
This is what the Firebase Realtime Database shows: Database
this is what my android emulator displays Emulator