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 !
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.
—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.
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 !