Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] java – Cards not updating after fetching data in Firestore

Posted on November 14, 2022

Solution 1 :

I notice you are calling adapter.notifyDataSetChanged() but I was not able to find where you are actually giving updated list of cats to your adapter.

You should adjust your adapter and do following changes-

Add setCatList() method

public void setCatList(List<String> catList) {
    this.catList= catList;
    notifyDataSetChanged();
}

Modify your getItemCount() method

@Override
public int getItemCount() {
    if (catList!= null)
        return catList.size();
    else
        return 0;
}

So structure of your InventoryAdapter will be as follow-

public class InventoryAdapter extends RecyclerView.Adapter<InventoryAdapter.CustomViewHolder> {

    private Context context;
    private List<String> catList;

    public InventoryAdapter(Context context) {
        this.context = context;
    }
    
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new CustomViewHolder(LayoutInflater.from(context).inflate(R.layout.cat_item_layout, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull CustomViewHolder holder, int position) {
    // setup binding of views
    }

    public void setCatList(List<String> catList) {
        this.catList= catList;
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        if (catList!= null)
            return catList.size();
        else
            return 0;
    }

    //inner class of CustomViewHolder
}

Then you there is no need to create new ArrayList<>() under onCreate() and onCreateView(). This is how you will create your adapter –

adapter = new InventoryAdapter(getContext());

and getCategories() will be as follows –

public void getCategories()
{
    db = FirebaseFirestore.getInstance();
    db.collection("categories")
            .get()
            .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                @Override
                public void onComplete(@NonNull Task<QuerySnapshot> task) {
                    if(task.isSuccessful())
                    {
                        List<String> cats = new ArrayList();
                        for(QueryDocumentSnapshot document : task.getResult()){
                            cats.add(document.get("categoryName").toString());
                            Log.d("fragment", document.get("categoryName").toString());
                        }
                        adapter.setCatList(cats ); //simply give updated cats list to recycle adapter
                    }
                }
            });
}

No need to call adapter.notifyDataSetChanged() because that will be taken care under setCatList() method of adapter.

If you want to preserve the catList then you can modify setCatList() accordingly as put a if check, if there is some data already or not as follows –

public void setCatList(List<String> catList) {
    if(this.catList!=null){
        if(this.catList.size()!=0){
            //there is some cat item inside already, take action accordingly 
        }
        else
            this.catList= catList;
    }
    else{
        this.catList= catList;
    }
   
    notifyDataSetChanged();
}

Happy Coding !

READ  [ANSWERED] flutter - Always shows "Indexing" alert while continuously adding assets in Android Studio
Powered by Inline Related Posts

Problem :

I am trying to write an app for an inventory management system and the one thing I need to do is to be able to display a list of categories that can be clicked on that will then take you to a list of products in that category. I am using a CardView with a RecyclerView, that both act on a fragment (as I am using a navigation drawer). I was able to get the cards to show successfully if a passed in a pre-defined ArrayList, but when I now fetch the data from Firestore, none of the cards want to appear, yet I am still using the same ArrayList, but the data is just coming from a different source. I am still a beginner in Android and would really appreciate it if someone could please help me. Here is my code for the Fragment class.

package com.example.drawerplayground;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

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

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.QueryDocumentSnapshot;
import com.google.firebase.firestore.QuerySnapshot;


import java.util.ArrayList;
import java.util.HashMap;

public class InventoryFragment extends Fragment {

    private TextView tv;
    private HashMap<String, Object> categories;
    private ArrayList<String> cats;
    private FirebaseFirestore db;
    private InventoryAdapter adapter;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cats = new ArrayList<>();
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View inventoryView = inflater.inflate(R.layout.fragment_inventory, container, false);
        RecyclerView recyclerView = (RecyclerView) inventoryView.findViewById(R.id.recycler_view);
        adapter = new InventoryAdapter(getContext(), cats);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(getActivity(), 2);
        recyclerView.setLayoutManager(gridLayoutManager);
        recyclerView.setAdapter(adapter);
        getCategories();
        return inventoryView;
    }

    public void getCategories()
    {
        cats = new ArrayList<>();
        db = FirebaseFirestore.getInstance();
        db.collection("categories")
                .get()
                .addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<QuerySnapshot> task) {
                        if(task.isSuccessful())
                        {
                            for(QueryDocumentSnapshot document : task.getResult())
                            {
                                cats.add(document.get("categoryName").toString());
                                Log.d("fragment", document.get("categoryName").toString());
                            }
                        }
                    }
                });
        adapter.notifyDataSetChanged();
    }


}

I tried using the notifyDataSetChanged() method for the adapter and also tried to perform the fetching of data in a separate function, as you can see above.

READ  [ANSWERED] android - Which Firebase javascript package should be used with React Native? The regular "Firebase Web SDK " or "react-native-firebase"?
Powered by Inline Related Posts

—EDIT—
Below you can see my basic idea of what the database structure looks like, I have a collection of categories, with each document being a different category and each document will contain an id and a categoryName field. It is a very basic and small structure for now, as my aim was to play around with the potential functionality before I started with the project itself.
enter image description here

Comments

Comment posted by Alex Mamo

Please edit your question and add your database structure as a screenshot.

Comment posted by James Foster

Nikhil Sharma was able to answer my question really well, but I still posted the screenshot of the database structure for context purposes.

Comment posted by James Foster

Thanks for the amazing help you gave, it made more sense to do what you did.

Comment posted by Nikhil Sharma

I believe programming is just another way of making sense :), glad you find it helpful … cheers mate !

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress