Search code examples
androidfirebase-realtime-databasegrid-layoutandroid-recyclerviewfirebaseui

How to implement override getItem() method in FirebaseRecyclerAdapter correctly


I am retrieving data from firebase database using FirebaseRecyclerAdapter. I am using a GridLayout to display the data. My data is ordered by push() keys in firebase database, and I would like to retrieve these data in reverse order. I have tried using

mLayoutManager.setReverseLayout(true);
mLayoutManager.setStackFromEnd(true);

Unfortunately, setStackFromEnd() is not supported for GridLayout. Without setStackFromEnd(), setReverseLayout() reverses the data order, but leaves a gap at the top, which is unacceptable.

However, I have found this answer

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

from another question (which I think may work) and have tried to implement it. However, I am getting an error: 'çannot resolve symbol Item', and 'attempting to use incompatible return type for getItem(int)'.

To be honest I do not know how to implement the getItem(). If anyone knows how to solve this please help! Thanks in advance!

Goal: to implement the getItem() code in an attempt to reverse the push() data retrieved from firebase database smoothly (without any gaps at the top). Assuming the code from the link works.

Code:

public class Browse extends Fragment {

    private FirebaseRecyclerAdapter<CategoryInformation, CategoryHolder> mFirebaseAdapter;

    private GridLayoutManager mLayoutManager;
    private RecyclerView rv;
    private DatabaseReference mDatabase;

    public Browse() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_browse, container, false);
        rv = rootView.findViewById(R.id.rv_recycler_view_browse);
        mLayoutManager = new GridLayoutManager(getContext(), 2);
        rv.setLayoutManager(mLayoutManager);

        mDatabase = FirebaseDatabase.getInstance().getReference().child("new");

        return rootView;
    }

    @Override
    public void onStart() {
        super.onStart();

        mFirebaseAdapter = new FirebaseRecyclerAdapter<CategoryInformation, CategoryHolder>(
                CategoryInformation.class,
                R.layout.card_item_browse,
                CategoryHolder.class,
                mDatabase) {

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

            @Override
            public void populateViewHolder(CategoryHolder holder, CategoryInformation chat, final int position) {
Picasso.with(getContext()).load(chat.getImage()).into(holder.feed_image);

                holder.rating.setText(chat.getRating());
            }
        };
        rv.setAdapter(mFirebaseAdapter);
    }

    //ViewHolder for our Firebase UI
    public static class CategoryHolder extends RecyclerView.ViewHolder{

        ImageView feed_image;
        TextView rating;
        View mView;

        public CategoryHolder(View v) {
            super(v);
            feed_image = v.findViewById(R.id.feed_image);
            rating = v.findViewById(R.id.rating);
            mView = v;
        }
    }
}

Solution

  • I found the answer. It was just a silly mistake. Item class is actually the model. So I renamed it to my model name CategoryInformation, and it was fixed!

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