I am trying to retrieve some data from Firebase Firestore although I am able to show the data on Logcat window but when I add it into my recycler view it doesn't show, recycler view doesn't inflate. I am unable to figure it out what I am currently missing although code runs without any error.
private FirebaseFirestore db = FirebaseFirestore.getInstance();
private CollectionReference sellerRef;
RecyclerView mRecyclerView;
List<Products> mProducts;
Adapter adapter;
String TAG="MyTag";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mRecyclerView = findViewById(R.id.mRecyclerView);
mProducts=new ArrayList<>();
GridLayoutManager gridLayoutManager = new GridLayoutManager(this,2,GridLayoutManager.VERTICAL,false);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(gridLayoutManager);
adapter = new Adapter(this,mProducts);
mRecyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
sellerRef=db.collection("Sellers");
Query query =sellerRef.whereEqualTo("City_Name","Sydney");
query.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
@Override
public void onComplete(@NonNull Task<QuerySnapshot> task) {
if (task.isSuccessful()){
for (QueryDocumentSnapshot document : task.getResult()) {
Products products =document.toObject(Products.class);
mProducts.add(products);
Log.d(TAG,""+products.getCity_Name()+" "+mProducts.size());
}
}else {
Toast.makeText(MainActivity.this, "Failed to load Data", Toast.LENGTH_SHORT).show();
}
}
});
}
}
Adapter
private Context mContext;
private List<Products> mProducts;
String TAG = "AdTag";
public Adapter(Context mContext, List<Products> mProducts) {
this.mContext = mContext;
this.mProducts= mProducts;
}
@NonNull
@Override
public Adapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(mContext).inflate(R.layout.grid_view_layout,parent,false);
return new Adapter.ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull Adapter.ViewHolder holder, int position) {
Products products=mProducts.get(position);
holder.title.setText(products.getCity_Name());
if (products.getThumbnailUrl().equals("default")){
holder.gridIcon.setImageResource(R.mipmap.ic_launcher);
} else {
Glide.with(mContext).load(products.getThumbnailUrl()).into(holder.gridIcon);
}
}
@Override
public int getItemCount() {
return mProducts.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView title;
public ImageView gridIcon;
public ViewHolder(@NonNull View itemView) {
super(itemView);
title=itemView.findViewById(R.id.textView2);
gridIcon=itemView.findViewById(R.id.imageView2);
}
}
}
Call adapter.notifyDataSetChanged();
after update data
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.