Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] java – recyclerViewAdapter.getItemCount() returning 0 even when it has objects inside?

Posted on November 11, 2022 By

Solution 1 :

There is no view created while you assign adapter to the recyclerView. My recommendation would be split logic between onCreateView and onViewCreated

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

   return inflater.inflate(R.layout.fragment_notice, container, false);
}

@Override
public onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
recyclerView = view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    FirebaseRecyclerOptions<Notice> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Notice>().setQuery(FirebaseDatabase.getInstance().getReference().child("Notice").child(Values.schoolCode).child(Values.student.getClass_txt()), Notice.class).build();

    recyclerViewAdapter = new RecyclerViewAdapter(firebaseRecyclerOptions);
    recyclerView.setAdapter(recyclerViewAdapter);
}

Problem :

I’m building a Fragment with a RecyclerView which displays notices retrieved from my database. Everything works fine, except for this line of code:

Problem:

recyclerViewAdapter.getItemCount()

This statement returns 0 even if the RecyclerViewAdapter has elements present. The full scenario is whenever the fragment loads, the Toast with the appropriate message pops up, but then after a slight delay, the first Notice appears from my database.

Question:

Is there a way with which the statement recyclerViewAdapter.getItemCount() gets called after all the fetching is done, as my interpretation says that the block gets executed as soon as the activity is created, not waiting for the adapter to confirm if there’s any Notice in the database or not?

Code Block:

Below is all the blocks of code which involve recyclerViewAdapter.

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

    view = inflater.inflate(R.layout.fragment_notice, container, false);
    recyclerView = view.findViewById(R.id.recycler_view);
    recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    FirebaseRecyclerOptions<Notice> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Notice>().setQuery(FirebaseDatabase.getInstance().getReference().child("Notice").child(Values.schoolCode).child(Values.student.getClass_txt()), Notice.class).build();

    recyclerViewAdapter = new RecyclerViewAdapter(firebaseRecyclerOptions);
    recyclerView.setAdapter(recyclerViewAdapter);

    if (recyclerViewAdapter.getItemCount() == 0) {
        Toast.makeText(getContext(), "No Notice Found", Toast.LENGTH_SHORT).show();
    }

    return view;
}

@Override
public void onStart() {
    super.onStart();
    recyclerViewAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    recyclerViewAdapter.stopListening();
}

Note: I have initialized the variable itself in the global scope so as to Override the appropriate methods later in the code.

Thanks in advance! 🙂

Comments

Comment posted by onDataChanged

The data retrieval is going to be asynchronous, so the short answer is “no, you can’t get the item count before returning from onCreateView”. If you want to check the size after it gets the data, you may be able to do so in

READ  [FIXED] xamarin.android - How can I place a word in the center of a splash screen in Android?
Powered by Inline Related Posts

Comment posted by Aayush Shukla

@TylerV Thanks for the comment, I got your point, and did it on the

Comment posted by Tyler V

While splitting it like this is recommended, the view definitely exists once you’ve called

Comment posted by Ihor Bykov

Yeah, you are right. The view will be created after inflate call.

Android Tags:android, android-recyclerview, firebase, firebase-realtime-database, java

Post navigation

Previous Post: [FIXED] android – W/Firestore: (24.1.2) [CustomClassMapper]: No setter/field for about found on class
Next Post: [FIXED] android – FusedLocationProviderClient and LocationCallback() object queries

Related Posts

[FIXED] java – How to play .ts live tv link in exoPlayer Android
[FIXED] android – Flutter : Several variant outputs are configured to use the same file name Android
[FIXED] android – Convert existing List into a json/text file so moving forward read the data from that file to generate the List Android
[FIXED] android – Unable to send periodic local notifications with WorkManager after app is killed Android
[FIXED] web – Unable to get event listener call back from WebView in Xamarin forms Android
[FIXED] android – How can I open gmail when click the button in jetpack compose? Android

Archives

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

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme