Problème avec recycler view
Sokatt
-
BunoCS Messages postés 15952 Date d'inscription Statut Modérateur Dernière intervention -
BunoCS Messages postés 15952 Date d'inscription Statut Modérateur Dernière intervention -
Bonjour,
J'ai suivi un tutoriel sur youtube pour la création de recycler view et cela ne fonctionne pas ai final, aucune erreur je n'ai juste pas d'affichage, c'est tout blanc.
Voici les codes :
PAGE POPULAIRE -----
ADAPTER ------
---- Offre (Variable et constructeur)
--- Layout Recycler
Layout ou l'affichage doit avoir lieu ----
Merci d'avance c'est pour un projet d'école et c'est vraiment stressant...
Bonne journée/soirée à tous.
J'ai suivi un tutoriel sur youtube pour la création de recycler view et cela ne fonctionne pas ai final, aucune erreur je n'ai juste pas d'affichage, c'est tout blanc.
Voici les codes :
PAGE POPULAIRE -----
package com.example.sharkprice_projet_bruno;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class populaire extends AppCompatActivity {
RecyclerView recyclerView;
private DatabaseReference ref;
private ArrayList<Offre> offreList;
private RecyclerAdapter recyclerAdapter;
private Context mContext;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_populaire);
recyclerView = findViewById(R.id.recyclerView);
LinearLayoutManager layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setHasFixedSize(true);
ref = FirebaseDatabase.getInstance().getReference();
offreList = new ArrayList<>();
clearAll();
GetDataFireBase();
BottomNavigationView navBottom = findViewById(R.id.navBar);
navBottom.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener(){
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
switch (menuItem.getItemId()){
case R.id.populaire:
startActivity(new Intent(populaire.this, populaire.class));
break;
case R.id.historique:
startActivity(new Intent(populaire.this, ListeArticle.class));
break;
case R.id.ajouter:
startActivity(new Intent(populaire.this, ajout_offre.class));
break;
}
return true;
}
});
}
private void GetDataFireBase(){
Query query = ref.child("Article");
query.addValueEventListener(new ValueEventListener(){
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
clearAll();
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
Offre offre = new Offre();
offre.setDescription(snapshot.child("Description").getValue().toString());
offre.setMagasin(snapshot.child("Magasin").getValue().toString());
offre.setNomArticle(snapshot.child("Nom_article").getValue().toString());
offre.setPrixReduit(snapshot.child("Prix_reduit").getValue().toString());
offreList.add(offre);
}
recyclerAdapter = new RecyclerAdapter(getApplicationContext(), offreList);
recyclerView.setAdapter(recyclerAdapter);
recyclerAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
private void clearAll(){
if (offreList != null){
offreList.clear();
if (recyclerAdapter != null ){
recyclerAdapter.notifyDataSetChanged();
}
}
offreList = new ArrayList<>();
}
}
ADAPTER ------
package com.example.sharkprice_projet_bruno;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private static final String Tag = "RecyclerView";
private Context mContext;
private ArrayList<Offre> listOffre;
public RecyclerAdapter(Context mContext, ArrayList<Offre> listOffre) {
this.mContext = mContext;
this.listOffre = listOffre;
}
@NonNull
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.offre_item, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
holder.desc.setText(listOffre.get(position).getDescription());
holder.mag.setText(listOffre.get(position).getMagasin());
holder.nomArt.setText(listOffre.get(position).getNomArticle());
holder.prix.setText(listOffre.get(position).getPrixReduit());
}
@Override
public int getItemCount(){
return listOffre.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
TextView desc;
TextView mag;
TextView nomArt;
TextView prix;
public ViewHolder(@NonNull View itemView) {
super(itemView);
desc = itemView.findViewById(R.id.desc);
mag = itemView.findViewById(R.id.mag);
nomArt = itemView.findViewById(R.id.nomArt);
prix = itemView.findViewById(R.id.prix);
}
}
}
---- Offre (Variable et constructeur)
package com.example.sharkprice_projet_bruno;
public class Offre {
String id;
String nomArticle;
String description;
String prixReduit;
String magasin;
String email;
public Offre(){
}
public Offre(String id, String nomArticle, String description, String prixReduit, String magasin, String email) {
this.id = id;
this.nomArticle = nomArticle;
this.description = description;
this.prixReduit = prixReduit;
this.magasin = magasin;
this.email = email;
}
public String getId(){
return id;
}
public void setId(String id) {
this.id = id;
}
public String getNomArticle(){
return nomArticle;
}
public void setNomArticle(String nomArticle) {
this.nomArticle = nomArticle;
}
public String getDescription(){
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getPrixReduit(){
return prixReduit;
}
public void setPrixReduit(String prixReduit) {
this.prixReduit = prixReduit;
}
public String getMagasin(){
return magasin;
}
public void setMagasin(String magasin) {
this.magasin = magasin;
}
public String getEmail(){
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
--- Layout Recycler
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:foreground="?android:attr/selectableItemBackground"
app:cardElevation="2dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textColor="#000000"
android:textSize="16sp" />
<TextView
android:id="@+id/mag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/desc"
android:layout_margin="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/nomArt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/mag"
android:layout_margin="10dp"
android:textSize="16sp" />
<TextView
android:id="@+id/prix"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/nomArt"
android:layout_margin="10dp"
android:textSize="16sp" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Layout ou l'affichage doit avoir lieu ----
<?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=".populaire">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/navBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#22427C"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/home_menu" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Merci d'avance c'est pour un projet d'école et c'est vraiment stressant...
Bonne journée/soirée à tous.
Configuration: Windows / Chrome 89.0.4389.114
A voir également:
- Problème avec recycler view
- Voir sa maison sur google street view - Guide
- View recovery logs - Guide
- Street view - Télécharger - Transports & Cartes
- Irfan view - Télécharger - Visionnage & Diaporama
- Wifi info view - Télécharger - Divers Réseau & Wi-Fi
1 réponse
Hello,
As-tu testé en mode debug, pas-à-pas, pour voir si tu récupères bien tes données et/ou si ton adapter reçoit bien les données ?
Généralement, on récupère et on affiche les données dans la callback
Pour savoir si c'est un pb d'interface, tu peux utiliser le Layout Inspector d'Android Studio.
As-tu testé en mode debug, pas-à-pas, pour voir si tu récupères bien tes données et/ou si ton adapter reçoit bien les données ?
Généralement, on récupère et on affiche les données dans la callback
onViewCreated(), pour être sûr que la
RecyclerViewsoit affichée.
Pour savoir si c'est un pb d'interface, tu peux utiliser le Layout Inspector d'Android Studio.