1

I have a Firebase database that looks something like this. It should be noted that I create the artCreated value by getting epoch time and subtracting it from zero:

enter image description here

I am trying to get the most recent submission, but I can never get the order to reverse. Here is my code:

public class RecentArtRecyclerViewAdapter extends RecyclerView.Adapter<RecentArtRecyclerViewAdapter.ArtworkViewHolder> {

    //List<ArtworkModel> artwork;

    private Context context;
    private DatabaseReference dbReference;
    private ChildEventListener mChildEventListener;

    ArrayList<String> artworkKeys = new ArrayList<>();
    ArrayList<ArtworkModel> artwork = new ArrayList<>();

    public RecentArtRecyclerViewAdapter(Context context, DatabaseReference dbReference){
        this.context = context;
        this.dbReference = dbReference;

        dbReference.limitToFirst(100);

        ChildEventListener childEventListener = new ChildEventListener() {
            @Override
            public void onChildAdded(DataSnapshot dataSnapshot, String s) {
                Log.d(TAG, "Artwork added...");

                ArtworkModel artworkModel = dataSnapshot.getValue(ArtworkModel.class);

                artwork.add(artworkModel);
                artworkKeys.add(dataSnapshot.getKey());
                notifyItemInserted(artwork.size()-1);
            }

I pass in the DB reference, which looks like:

 DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
    final DatabaseReference artRef =  databaseReference.child("artwork");

I have tried to get the reverse order (most recent) by doing things like:

dbReference.limitToFirst(100);
dbReference.limitToLast(100);
dbReference.orderByChild("artCreated");
dbReference.orderByChild("artCreated").limitToLast(10);
dbReference.orderByChild("artCreated").limitToFirst(10);

I have also tried changing the value of the artCreated which started with everything being a negative number (smallest to largest) and also doing one negative number, and the rest positive (largest to smallest) and got the same result. The artCreated key seems to have no impact on the order.

And nothing is working. It is always in the same order that it is stored in the database. What am I doing wrong. I have referenced a number of answers on this, but nothing is working for me.

AL.
  • 36,815
  • 10
  • 142
  • 281
BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156

1 Answers1

0

Someone has pointed out that there are 2 ways to do this:

  1. Manipulate the data client-side
  2. Make a query that will order the data

The easiest way that I have found to do this is to use option 1, but through a LinkedList. I just append each of the objects to the front of the stack. It is flexible enough to still allow the list to be used in a ListView or RecyclerView. This way even though they come in order oldest to newest, you can still view, or retrieve, newest to oldest.

BlackHatSamurai
  • 23,275
  • 22
  • 95
  • 156