Search code examples
androidfirebasefirebase-realtime-databasefirebaseui

Is there any simple way to make FirebaseRecyclerAdapter for implementation 'com.firebaseui:firebase-ui-database:4.0.1'


Let me get straight to the point. I have searched many times about RECYCLE VIEW and CARD VIEW. I have already tried many tips and yet I still can't find out which one is the best and the latest.

I have the feeling that this one firebase-ui (FirebaseRecyclerAdapter) is the easiest one to implement in recycle view and card view.

"implementation 'com.firebaseui:firebase-ui-database:4.0.1'"

What I found out was an error at

@Override  //It says 'Method does not override from its superclass.
protected void populateViewHolder(HomeViewHolder homeViewHolder, home model, int position){
homeViewHolder.setTitle(model.getTitle());
homeViewHolder.setShortDescription(model.getShortDescription());
}

I think this one is not available for the latest update for 4.0.1

I put the code in Fragment.

FragmentHome.java

package my.package.sports;


import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


/**
 * A simple {@link Fragment} subclass.
 */
public class FragmentHome extends Fragment {

    private FirebaseRecyclerAdapter firebaseRecyclerAdapterHome;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.fragment_home, container, false);
        DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference().child("home");
        databaseReference.keepSynced(true);
        RecyclerView recyclerView = mainView.findViewById(R.id.recycleView);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        //FirebaseRecyclerAdapter
        firebaseRecyclerAdapterHome = new FirebaseRecyclerAdapter<home, HomeViewHolder>(home.class, R.layout.home_row, HomeViewHolder.class, databaseReference) {
            @NonNull
            @Override
            public HomeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return null;
            }

            @Override
            protected void onBindViewHolder(HomeViewHolder holder, int position, home model) {
            }

            @Override  //It says 'Method does not override from its superclass.(Only Here the Error)
            protected void populateViewHolder(HomeViewHolder homeViewHolder, home model, int position) {
                homeViewHolder.setTitle(model.getTitle());
                homeViewHolder.setShortDescription(model.getShortDescription());
            }
        };
        recyclerView.setAdapter(firebaseRecyclerAdapterHome);

        return mainView;
    }

    public static class HomeViewHolder extends RecyclerView.ViewHolder {
        View viewData;
        public HomeViewHolder(View viewItem) {
            super(viewItem);
            viewData = viewItem;
        }
        public void setTitle(String title) {
            TextView textViewTitle = viewData.findViewById(R.id.home_row_title);
            textViewTitle.setText(title);
        }
        public void setShortDescription(String shortDescription) {
            TextView textViewShortDescription = viewData.findViewById(R.id.home_row_short_description);
            textViewShortDescription.setText(shortDescription);
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        firebaseRecyclerAdapterHome.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        firebaseRecyclerAdapterHome.stopListening();
    }
}

home.class

package my.package.sports;

public class home {

    private String title;
    private String shortDescription;

    home() {

    }

    public home(String title, String shortDescription) {
        this.title = title;
        this.shortDescription = shortDescription;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getShortDescription() {
        return shortDescription;
    }

    public void setShortDescription(String shortDescription) {
        this.shortDescription = shortDescription;
    }
}

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".FragmentHome">

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

</FrameLayout>

home_row.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/cardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_margin="7dp"
    android:elevation="90dp"
    android:orientation="vertical"
    app:cardCornerRadius="11dp">


    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="245dp"
        android:background="@color/colorPrimaryDark"
        android:orientation="vertical">

        <TextView
            android:id="@+id/home_row_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="7dp"
            android:text="Title"
            android:textColor="@color/colorText"
            android:textSize="20sp" />

        <TextView
            android:id="@+id/home_row_short_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingLeft="7dp"
            android:text="Short Description" />

    </LinearLayout>
</android.support.v7.widget.CardView>

Please help me to make recycleView and cardView for Fragment and apply to firebase.


Solution

  • I have solved my question by downgrading the firebaseui.

    implementation 'com.firebaseui:firebase-ui-database:0.4.0'

    And I use populateViewHolder as it supports for 0.4.0

     FirebaseRecyclerAdapter<home, HomeViewHolder> firebaseRecyclerAdapterHome =
                    new FirebaseRecyclerAdapter<home, HomeViewHolder>(
                            home.class,
                            R.layout.home_row,
                            HomeViewHolder.class,
                            databaseReference
                    ) {
                        @Override
                        protected void populateViewHolder(HomeViewHolder viewHolder, home model, int position) {
                            viewHolder.setDetails(getContext(), model.getImage(),model.getTitle(), model.getDate(), model.getShortDescription());
                        }
                    };
            //setAdapter
            recyclerView.setAdapter(firebaseRecyclerAdapterHome);