简体   繁体   中英

RecyclerView in a Fragment with CardView and Firebase

I'm trying to use RecyclerView in a Fragment with CardView and Firebase (the database has all the images, name and quantity to pass in the Card) but i have a problem:

Nothing displays in my fragment, My RecyclerView don't work and my card don't dispalys. my fragment is empty and i have no error in Log.

Here is the Java of the Fragment:

package com.example.progettocontab.assistenza;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.progettocontab.R;

import com.example.progettocontab.app.Articolo;
import com.example.progettocontab.app.ArticoloAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.FirebaseDatabase;

public class FragmentAssistenza extends Fragment {

    RecyclerView recyclerView;
    ArticoloAdapter adapter;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_assistenza, container, false);

        recyclerView = (RecyclerView) view.findViewById(R.id.recview);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));

        FirebaseRecyclerOptions<Articolo> options =
                new FirebaseRecyclerOptions.Builder<Articolo>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("Articoli"), Articolo.class)
                        .build();

        adapter = new ArticoloAdapter(options);
        recyclerView.setAdapter(adapter);

        return view;
    }

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

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

Here is the XML of Fragment:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".assistenza.FragmentAssistenza">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginStart="1dp"
        android:layout_marginLeft="1dp"
        android:layout_marginTop="1dp"
        android:layout_marginEnd="1dp"
        android:layout_marginRight="1dp"
        android:layout_marginBottom="1dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is the Adapter (with the ViewHolder class):

package com.example.progettocontab.app;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.progettocontab.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;


public class ArticoloAdapter extends FirebaseRecyclerAdapter<Articolo, ArticoloAdapter.ViewHolder> {

    public ArticoloAdapter(@NonNull FirebaseRecyclerOptions<Articolo> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, final int position, @NonNull Articolo model) {
        holder.nome.setText(model.getNome());
        holder.quantita.setText(model.getQuantita());
        Glide.with(holder.img.getContext()).load(model.getImg()).into(holder.img);

        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(v.getContext(), model.getNome(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_article, parent, false);
        return new ViewHolder(view);
    }

    class ViewHolder extends RecyclerView.ViewHolder {

        ImageView img;
        TextView nome;
        TextView quantita;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            img = (ImageView) itemView.findViewById(R.id.img);
            nome = (TextView) itemView.findViewById(R.id.nameText);
            quantita = (TextView) itemView.findViewById(R.id.amountText);
        }

    }

}

Here is the Java of Articolo:

package com.example.progettocontab.app;

public class Articolo {

    private String nome;
    private String descrizione;
    private String quantita;
    private String img;
    private int ID;

    public Articolo(String nome, String descrizione, String quantita, String img, int ID) {
        this.nome = nome;
        this.descrizione = descrizione;
        this.quantita = quantita;
        this.img = img;
        this.ID = ID;
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String getQuantita() {
        return quantita;
    }

    public void setQuantita(String quantita) {
        this.quantita = quantita;
    }

    public String getImg() {
        return img;
    }

    public void setImg(String img) {
        this.img = img;
    }

    public int getID() {
        return ID;
    }

    public void setID(int ID) {
        this.ID = ID;
    }
}

And here is the XML of the CardView:

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:cardCornerRadius="5dp"
    android:elevation="5dp"
    app:cardUseCompatPadding="true">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="15dp">

        <androidx.appcompat.widget.AppCompatImageView
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:src="@mipmap/ic_launcher"
            android:layout_centerVertical="true"
            android:id="@+id/img"
            />

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/nameText"
            android:text="This is Demo Name"
            android:textStyle="bold"
            android:textSize="25sp"
            android:textColor="#000"
            android:layout_toRightOf="@id/img"
            android:layout_marginLeft="10dp"/>

        <TextView
            android:id="@+id/amountText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/nameText"
            android:layout_marginLeft="6dp"
            android:layout_marginTop="12dp"
            android:layout_toRightOf="@id/img"
            android:text="This is Course Name"
            android:textColor="#000"
            android:textSize="25sp" />


    </RelativeLayout>

</androidx.cardview.widget.CardView>

I think you might be getting that error because you have implementation 'com.google.firebase:firebase-messaging' in your module's gradle, but you haven't registered the service in your manifest. So either remove that line from your gradle (if you don't need the messaging functionality of firebase), or add the service to your manifest as follows:

<service
    android:name=".java.MyFirebaseMessagingService"
    android:exported="false">
    <intent-filter>
        <action android:name="com.google.firebase.MESSAGING_EVENT" />
    </intent-filter>
</service>

Try to move recyclerView.setAdapter(adapter) after you startListening

So, remove it from onViewCreated() , and add it to onStart()

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();
    recyclerView.setAdapter(adapter);
}

Try shifting recyclerview to onViewCreated because at that point, the view of fragment gets populated and you can access recyclerview and inflate it.

@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
   View view = inflater.inflate(R.layout.fragment_assistenza, container, false);
   return view;
}

@Override
public void onViewCreated() {
   recyclerView = (RecyclerView) view.findViewById(R.id.recview);
   recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));

   FirebaseRecyclerOptions<Articolo> options =
                new FirebaseRecyclerOptions.Builder<Articolo>()
                        .setQuery(FirebaseDatabase.getInstance().getReference().child("Articoli"), Articolo.class)
                        .build();

   adapter = new ArticoloAdapter(options);
   recyclerView.setAdapter(adapter);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM