Home » Android » java – Android – FirebaseListAdapter in reverse order?

java – Android – FirebaseListAdapter in reverse order?

Posted by: admin June 15, 2020 Leave a comment

Questions:

I currently have a FirebaseListAdapter that populates a ListView with the last 5 items:

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    FirebaseListAdapter<HashMap> adapter = new FirebaseListAdapter<HashMap>(getParentFragment().getActivity(),HashMap.class,R.layout.list_item,firebase.limitToLast(5)) {
        @Override
        protected void populateView(View view, HashMap hashMap, int i) {
            String title = hashMap.get("title").toString();
            String body = hashMap.get("body").toString();

            TextView title_txt = (TextView)view.findViewById(R.id.title);
            TextView body_txt = (TextView)view.findViewById(R.id.body);

            title_txt.setText(title);
            body_txt.setText(body);
        }
    };

    listView.setAdapter(adapter);
}

The problem I have is that when a new item is pushed to Firebase, it is added to the bottom of the list. I want the newest items at the top of the list.

Is it possible to achieve this?

Any help is greatly appreciated, thanks!

How to&Answers:

This is not the exact solution of your problem of getting the data from firebase in a reverse order, but anyway, we’ve other work arounds.

The first work around is mentioned in a comment of using a custom adapter for your list.

To achieve the behaviour you need to get the firebase data in a list and then you’ve to reverse it yourself before passing it to an adapter. Simple!

The second work around is easy as pie and I think if you’re using RecyclerView it could do the trick for you and I see it as the simplest way you can do the job.

Here’s I’m modifying some of my code with RecyclerView

// Declare the RecyclerView and the LinearLayoutManager first
private RecyclerView listView;
private LinearLayoutManager mLayoutManager;

@Override
public void onViewCreated(View view, Bundle savedInstanceState){

    // Use FirebaseRecyclerAdapter here

    // Here you modify your LinearLayoutManager
    mLayoutManager = new LinearLayoutManager(MainActivity.this);
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);

    // Now set the layout manager and the adapter to the RecyclerView
    listView.setLayoutManager(mLayoutManager);
    listView.setAdapter(adapter);
}

By setting mLayoutManager.setReverseLayout(true); – you’re reversing your layout and mLayoutManager.setStackFromEnd(true); positions the view to the top of your list.

Migrating to RecyclerView is simple. Your layout will be something like this

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_list"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
</RelativeLayout>

And in your build.gradle

dependencies {
    compile 'com.android.support:recyclerview-v7:23.4.0'
}

You need to have FirebaseRecyclerAdapter can be found here in FirebaseUI library.

Note: Do not use RecyclerView.LayoutManager as the setReverseLayout and setStackFromEnd functions won’t be found in RecyclerView.LayoutManager. Use LinearLayoutManager as stated.

Update

Here’s how you can handle the click events of your items in the list.

You had to declare a ViewHolder to implement the RecyclerView right? Just add another function inside your ViewHolder class like the example below and call this function after the setText functions you’ve got there.

public static class MyViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public MyViewHolder(View itemView) {
        super(itemView);
        mView = itemView;
    }

    public void setClickEvent() {
        // Set the onClickListener on mView
        // mView.setOnClickListener(new OnClickListener)...
    }
}

Answer:

Since you’re already extending FirebaseListAdapter to implement populateView, you can go ahead and override getItem as well to invert the item lookup:

@Override
public HashMap getItem(int pos) {
    return super.getItem(getCount() - 1 - pos);
}

If you’re not using RecyclerView this is a simple way to reverse the data without extending any more classes than necessary. Obviously this will reverse any AdapterView subclass backed by this adapter, if you choose to reuse it.