Cardview et recyclerview

Résolu/Fermé
Eche043 Messages postés 16 Date d'inscription mercredi 20 septembre 2017 Statut Membre Dernière intervention 30 mai 2020 - 8 déc. 2019 à 19:16
Eche043 Messages postés 16 Date d'inscription mercredi 20 septembre 2017 Statut Membre Dernière intervention 30 mai 2020 - 9 déc. 2019 à 11:10
Bonjour à tous,

je suis un novice en développement android (je code avec Android Studio). Mon problème est que je veux afficher des information qui se trouve au niveau de Firebase grâce à cardview et recyclerview.

quand je test sur AVD sa passe bien, mais sur mon smartphone réel les information peuvent mettre des heures avant de s'afficher. souvent rien ne s'affiche.

bon les images expliquent mieux ce que je veux dire


sur l'emulateur tout est au top



sur le telephone rien ne s'affiche je dois patienter des heures souvent avant que tout sois ok, souvent rien ne s'affiche jamais.

mon code

activity_homepage.xml
<ScrollView
    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=".Homepage">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

    <include layout="@layout/toolbar_activity"/>

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="65dp"/>
    </RelativeLayout>


</ScrollView>


Homepage.java
package com.ci.phonemarket;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import io.paperdb.Paper;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import com.ci.phonemarket.Modele.Product;
import com.ci.phonemarket.ViewHolder.ProductViewHolder;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Homepage extends AppCompatActivity {

    private DatabaseReference ProductRef;
    private RecyclerView recyclerView;
    RecyclerView.LayoutManager layoutManager;
    FirebaseRecyclerAdapter<Product, ProductViewHolder> adapter;

    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        toolbar = (Toolbar) findViewById(R.id.toolbar2);
        setSupportActionBar(toolbar);

        ProductRef = FirebaseDatabase.getInstance().getReference().child("Produits");
        recyclerView = (RecyclerView) findViewById(R.id.recycler);
        recyclerView.setHasFixedSize(true);
        layoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);

        showList();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.deconnextion){
            Intent intent = new Intent(Homepage.this, MainActivity.class);
            startActivity(intent);
            finish();
            Paper.book().destroy();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_toolbar, menu);
        return super.onCreateOptionsMenu(menu);
    }




private void showList(){
    FirebaseRecyclerOptions options =
    new FirebaseRecyclerOptions.Builder<Product>()
            .setQuery(ProductRef, Product.class)
            .build();


    adapter = new FirebaseRecyclerAdapter<Product, ProductViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull ProductViewHolder productViewHolder, int position, @NonNull Product product) {
            productViewHolder.txtproductName.setText(product.getNom());
            productViewHolder.txtQuantity.setText(product.getQuantite() + "Kg");
            productViewHolder.txtProductDescription.setText(product.getDescription());
        }

        @NonNull
        @Override
        public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
            View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.products_items_layout, viewGroup,false);
            return new ProductViewHolder(view);
        }
    };
    adapter.startListening();
    adapter.notifyDataSetChanged();
    recyclerView.setAdapter(adapter);
}
}



products_item_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="20dp"
    android:layout_marginStart="15dp"
    android:layout_marginEnd="15dp"
    app:cardElevation="15dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <TextView
            android:id="@+id/product_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Nom du produit"
            android:textAlignment="center"
            android:textSize="20dp"
            android:textStyle="bold"
            android:textColor="@color/colorPrimaryDark"/>

        <ImageView
            android:id="@+id/product_image"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:layout_below="@+id/product_name"
            android:scaleType="center"
            android:layout_marginTop="2dp"/>

        <LinearLayout
            android:id="@+id/quantite_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:layout_below="@+id/product_image">
            <TextView

                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="quantité disponible"
                android:layout_marginTop="4dp"

                android:textSize="15dp"
                android:textStyle="bold"
                android:textColor="@color/rouge"/>
            <TextView
                android:id="@+id/product_quantity"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_marginLeft="5dp"
                android:textStyle="bold"
                android:text="nombre"/>

        </LinearLayout>

        <TextView
            android:id="@+id/product_description"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/quantite_layout"
            android:text="Description du produit"
            android:textAlignment="center"
            android:textSize="20dp"
            android:textColor="@color/colorPrimary"/>

    </RelativeLayout>



</androidx.cardview.widget.CardView>


Product.java
package com.ci.phonemarket.Modele;

public class Product {

    private String quantite, nom_planteur, nom, description, dates, heures;

    public Product(){

    }

    public Product(String quantite, String nom_planteur, String nom, String description, String dates, String heures) {
        this.quantite = quantite;
        this.nom_planteur = nom_planteur;
        this.nom = nom;
        this.description = description;
        this.dates = dates;
        this.heures = heures;
    }


    public String getQuantite() {
        return quantite;
    }

    public void setQuantite(String quantite) {
        this.quantite = quantite;
    }

    public String getNom_planteur() {
        return nom_planteur;
    }

    public void setNom_planteur(String nom_planteur) {
        this.nom_planteur = nom_planteur;
    }

    public String getNom() {
        return nom;
    }

    public void setNom(String nom) {
        this.nom = nom;
    }


    public String getDescription() {
        return description;
    }

    public void setDescription(String description) {
        this.description = description;
    }

    public String getDates() {
        return dates;
    }

    public void setDates(String dates) {
        this.dates = dates;
    }

    public String getHeures() {
        return heures;
    }

    public void setHeures(String heures) {
        this.heures = heures;
    }
}



ProductViewHolder.java
package com.ci.phonemarket.ViewHolder;

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

import com.ci.phonemarket.Interface.itemClickListener;
import com.ci.phonemarket.R;

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

public class ProductViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView txtproductName, txtProductDescription, txtQuantity;
    public ImageView imageView;
    public itemClickListener listener;


    public ProductViewHolder(@NonNull View itemView) {
        super(itemView);

        imageView = (ImageView) itemView.findViewById(R.id.product_image);
        txtproductName = (TextView) itemView.findViewById(R.id.product_name);
        txtProductDescription = (TextView) itemView.findViewById(R.id.product_description);
        txtQuantity = (TextView) itemView.findViewById(R.id.product_quantity);
    }

    public void setitemClickListener(itemClickListener listener){
        this.listener = listener;
    }

    @Override
    public void onClick(View view) {
        listener.onClick(view, getAdapterPosition(), false );
    }
}



Merci

1 réponse

BunoCS Messages postés 15472 Date d'inscription lundi 11 juillet 2005 Statut Modérateur Dernière intervention 25 mars 2024 3 894
9 déc. 2019 à 09:19
Salut,

As-tu regardé dans les logs pour voir si tu as un crash ou un message d'erreur ?
Je ne vois pas où tu fais ta requête Firebase ni où tu récupères tes données...

A noter qu'on ne met pas une
RecyclerView
dans une
ScrollView
... C'est source de bug d'intégrer un composant scrollable dans un autre composant scrollable...
1
Eche043 Messages postés 16 Date d'inscription mercredi 20 septembre 2017 Statut Membre Dernière intervention 30 mai 2020
9 déc. 2019 à 10:05
Merci pour ta réponse BunoCS le log me donne aucune erreur. je retire le ScrollView et je vous reviens
0
Eche043 Messages postés 16 Date d'inscription mercredi 20 septembre 2017 Statut Membre Dernière intervention 30 mai 2020
9 déc. 2019 à 11:10
problème résolu après avoir retiré le ScrollView. Merci BunoCS
0