FirebaseListAdapter in reverse order?

I currently have a FirebaseListAdapter that populates the ListView 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); } 

My problem is that when a new item is placed in Firebase, it is added to the end of the list. I want the newest items at the top of the list.

Can this be achieved?

Any help greatly appreciated, thanks!

+7
source share
2 answers

This is not an exact solution to your problem of receiving data from firebase in the reverse order, but in any case, we have other problems.

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

To achieve this behavior, you need to get the database data in the list, and then you must cancel it yourself before transferring it to the adapter. Just!

The second job is as easy as pie, and I think that if you use RecyclerView , this can do the trick for you, and I believe that this is the easiest way to get the job done.

Here I am changing part of my code using 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 change the layout, and mLayoutManager.setStackFromEnd(true); Positions the view at the top of the list.

Switching 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 the FirebaseRecyclerAdapter be found here in the FirebaseUI library.

Note. Do not use RecyclerView.LayoutManager , as the setReverseLayout and setStackFromEnd functions will not be found in RecyclerView.LayoutManager . Use LinearLayoutManager as LinearLayoutManager .

Update

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

Did you need to declare a ViewHolder for the correct implementation of RecyclerView ? Just add another function to your ViewHolder class, as shown below, and call this function after the setText functions that you have.

 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)... } } 
+14
source

Since you are already extending FirebaseListAdapter to implement populateView , you can continue and override getItem to invert the element search:

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

If you are not using RecyclerView , this is an easy way to modify data without extending any classes than necessary. Obviously, this will change any subclass of AdapterView supported by this adapter if you decide to reuse it.

+5
source

All Articles