Home » Java » java – FirebaseRecyclerAdapter is not displaying data in my RecyclerView layout-Exceptionshub

java – FirebaseRecyclerAdapter is not displaying data in my RecyclerView layout-Exceptionshub

Posted by: admin February 25, 2020 Leave a comment

Questions:

Please note there are lot of questions like these but none of them worked for me.
I have been trying to display data in a recycler view and it isnt displaying the data and not throwing any errors
Here is my code.

I have included the latest version of Firebase Dependencies and Latest version of Firebase UI Database

Dependencies Version

// Firebase
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.1'
implementation 'com.google.firebase:firebase-storage:19.1.1'

// Firebase UI
implementation 'com.firebaseui:firebase-ui-database:6.0.2'

This is the Films class
Films.java

package com.dipeshpatil.in;

public class Films {

    public String name, year;

    public Films(String name, String year) {
        this.name = name;
        this.year = year;
    }

    public Films() {

    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getYear() {
        return year;
    }

    public void setYear(String year) {
        this.year = year;
    }
}

This is the ViewHolder Class FilmsViewHolder.java

package com.dipeshpatil.in;

import android.view.View;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

public class FilmsViewHolder extends RecyclerView.ViewHolder {
    public TextView mFilmName;
    public TextView mFilmYear;

    public FilmsViewHolder(View itemView){
        super(itemView);

        mFilmName = itemView.findViewById(R.id.user_single_name);
        mFilmYear = itemView.findViewById(R.id.user_single_status);
    }
}

This is the activity where i am supposed to display the data in recycler view FilmsActivity.java

public class FilmsActivity extends AppCompatActivity {

    private RecyclerView mFilmsList;
    private DatabaseReference mFilmsDatabase;
    private FirebaseRecyclerOptions<Films> options;
    private FirebaseRecyclerAdapter<Films, FilmsViewHolder> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_films);

        mFilmsList = findViewById(R.id.films_list);
        mFilmsList.setHasFixedSize(true);

        mFilmsDatabase = FirebaseDatabase.getInstance().getReference().child("Films");

        options = new FirebaseRecyclerOptions.Builder<Films>()
                .setQuery(mFilmsDatabase, Films.class)
                .build();

        adapter = new FirebaseRecyclerAdapter<Films, FilmsViewHolder>(options) {
            @Override
            protected void onBindViewHolder(FilmsViewHolder filmsViewHolder, int i, Films films) {

                filmsViewHolder.mFilmName.setText(films.getName());
                filmsViewHolder.mFilmYear.setText(films.getYear());
            }

            @NonNull
            @Override
            public FilmsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

                View mView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.users_single_layout, parent, false);

                return new FilmsViewHolder(mView);
            }
        };
        mFilmsList.setAdapter(adapter);
    }

    @Override
    protected void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

Now honestly i am not getting any kind of error which would help me debug the code and check for problems and at the same time it is also not displaying my desired output

Firebase Database for Films

"Films" : {
    "01" : {
      "name" : "The Prestige",
      "year" : 2002
    },
    "02" : {
      "name" : "Fight Club",
      "year" : 1999
    }
}

Layout File activity_films.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:orientation="vertical"
    tools:context=".FilmsActivity">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="16dp"
        android:id="@+id/films_list" />
</LinearLayout>
How to&Answers:

com.google.firebase.database.DatabaseException: Failed to convert
value of type java.lang.Long to String

First of all, it’s should be a InvocationTargetException [according your database structure] as you try to cast Long as String in your model. Try to update your model like below:

public class Films {
    public String name;
    public int year;

    ....
}

Then, You should set LayoutManager to your RecyclerView

LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
mFilmsList.setLayoutManager(linearLayoutManager);

Update: You have to update your onBindViewHolder to convert int to String before setting it to TextView using String.valueOf

@Override
protected void onBindViewHolder(FilmsViewHolder filmsViewHolder, int i, Films films) {

    filmsViewHolder.mFilmName.setText(films.getName());
    filmsViewHolder.mFilmYear.setText(String.valueOf(films.getYear()));
}

Answer:

I think there’s usually a delay in when you get a response back from the Firebase query. Thats probably whats happening. By the time you initialize the adapter, the “options” list is still empty.

You will most likely need to use thread synchronization to do this.