0

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

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193

1 Answers1

1

I'm sure I need to add a timestamp to fix it, I'm not 100% sure though

Indeed you need to add a new property that can hold a timestamp. This can be done using my answer from the following post:

Once you have the new fields in place, you can then use Query's orderByChild(String path) method:

Creates a query in which child nodes are ordered by the values of the specified path.

By default, the order is ascending. If you need a descending order, please see my answer from the following post:

How to arrange firebase database data in ascending or descending order?

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193