Reputation: 1333
I have a recycler view in a HomeFragment
(of MainActivity), populated with a FirestoreRecyclerAdapter
and a product card view, which has an onClick
event that opens a ProductActivity
.
The recyclerview displays the items fine when first opened. but if one clicks on one of the items, which opens the Product Activity and then press the back button to come back to Homefragment
, the recycler view is empty
here is my Fragment:
public class ExploreFragment extends Fragment {
public static final String TAG = "ExploreFragment";
Context context;
View rootView;
@BindView(R.id.toolbar)
Toolbar toolbar;
@BindView(R.id.rv_categories)
RecyclerView rv_categories;
@BindView(R.id.btn_more_categories)
MaterialButton btn_more_categories;
RecyclerView.Adapter mCategoryAdapter;
LinearLayoutManager mCategoryLayoutManager;
@BindView(R.id.rv_ads_nearby)
RecyclerView rv_ads_nearby;
FirestoreRecyclerAdapter mAdsAdapter;
StaggeredGridLayoutManager mAdsLayoutManager;
public ExploreFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
rootView = inflater.inflate(R.layout.fragment_explore, container, false);
ButterKnife.bind(this,rootView);
context = this.getContext();
((MainActivity) getActivity()).setToolbar(toolbar,"The Marketplace",false);
setupCategories();
setupAds();
return rootView;
}
private void setupCategories(){
mCategoryLayoutManager = new LinearLayoutManager(context,RecyclerView.HORIZONTAL,false);
mCategoryAdapter = new CategoryAdapter(Express.categories(),context);
rv_categories.setLayoutManager(mCategoryLayoutManager);
rv_categories.setAdapter(mCategoryAdapter);
Log.d("CATE","Categories: "+mCategoryAdapter.getItemCount());
Log.d("CATE","Categories Express: "+Express.categories().size());
}
private void setupAds(){
mAdsLayoutManager = new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL);
rv_ads_nearby.setLayoutManager(mAdsLayoutManager);
// Set up FirebaseRecyclerAdapter with the Query
Query adsQuery = Express.firestore.collection(FS_ADS).orderBy("postedAt",DESCENDING);
// Firestore options
FirestoreRecyclerOptions options = new FirestoreRecyclerOptions.Builder<Ad>()
.setQuery(adsQuery, Ad.class)
.build();
mAdsAdapter = new FirestoreRecyclerAdapter<Ad, AdViewHolder>(options) {
@NonNull
@Override
public AdViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ad_card, parent, false);
return new AdViewHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull AdViewHolder adViewHolder, int i, @NonNull Ad ad) {
adViewHolder.bindToAd(ad, getActivity(), new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), AdActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra("AD",ad);
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return super.getItemCount();
}
@Override
public void onError(FirebaseFirestoreException e) {
Log.e(TAG, e.getMessage());
}
};
rv_ads_nearby.setAdapter(mAdsAdapter);
mAdsAdapter.notifyDataSetChanged();
}
@Override
public void onStart() {
super.onStart();
if (mAdsAdapter != null) {
mAdsAdapter.startListening();
}
}
@Override
public void onStop() {
super.onStop();
if (mAdsAdapter != null) {
mAdsAdapter.stopListening();
mAdsAdapter = null;
}
}
}
Upvotes: 1
Views: 1697
Reputation: 1333
Figured it out, I was setting the adapter to null in the onStop() method of the fragment. so just removed this line mAdsAdapter = null;
from onStop()
also added mAdsAdapter.notifyDataSetChaged()
and mAdsAdapter.startListening();
Thank you for your help guys!
Upvotes: 1