Does the FireStoreRecycler or Query make a asynchronous call to the database?

Viewed 18

I've been fiddling around with the Firebase Firestore and have been using the FireStoreUI library to facilitate displaying data from my database in my RecyclerView.

However, I noticed that by using the Query object and it's orderBy function, I am able to only display a single value from my Array that's inside the database.

I then proceeded to add:

@Override
   public int getItemCount() {
       Log.e("TITLE","result" + super.getItemCount());
       return super.getItemCount();
   }

In my FirestoreAdapter class. For a few seconds, I get a result of 0, and then I get a result of 1. As in the screenshot:

Result

And here's my Firestore Database:

Database

Which makes me wonder, is this why my RecyclerView only displays the first value of the array? Do I have to create some sort of callBack function? Because most documentation/tutorials I've seen about it don't have one.

Here's my Activity

public class OsActivity extends AppCompatActivity {
    private FirestoreAdapter listAdapter;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference osRef = db.collection("cliente");
    private DocumentReference docRef = osRef.document("clienteTeste");

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_os);

        RecyclerView recyclerView = findViewById(R.id.recyclerViewOs);
        recyclerView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
        recyclerView.setHasFixedSize(true);

        /*docRef.get().addOnCompleteListener(task -> {
            if (task.isSuccessful()) {
                DocumentSnapshot document = task.getResult();
                if (document.exists()) {
                    Log.e("RESULTADO","result:" + document.toString());
                }
            }
        });*/

        Query query = osRef.orderBy("serviceOrder",
                Query.Direction.ASCENDING);

        FirestoreRecyclerOptions<ServiceDocument> options =
                new FirestoreRecyclerOptions.Builder<ServiceDocument>()
                        .setQuery(query, ServiceDocument.class)
                        .build();

         listAdapter = new FirestoreAdapter(options);
        recyclerView.setAdapter(listAdapter);
        listAdapter.notifyDataSetChanged();

    }

    @Override
    protected void onStart() {
        super.onStart();
        listAdapter.startListening();
    }

    @Override
    protected void onStop() {
        super.onStop();
        listAdapter.stopListening();
    }

And here's my Adapter:

public class FirestoreAdapter extends FirestoreRecyclerAdapter<ServiceDocument, FirestoreAdapter.ViewHolder> {

    public FirestoreAdapter(@NonNull FirestoreRecyclerOptions<ServiceDocument> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull ServiceDocument model) {
        holder.osIdItem.setText(String.valueOf(model.serviceOrder.get(position).getId()));
        holder.osClientItem.setText(model.serviceOrder.get(position).getClient().getName());
        holder.osDateItem.setText(model.serviceOrder.get(position).getDate());
        holder.osValueItem.setText(String.valueOf(model.serviceOrder.get(position).getTotalValue()));
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());

        View view = layoutInflater.inflate(R.layout.layout_os_item, parent, false);

        return new FirestoreAdapter.ViewHolder(view);
    }

    @Override
    public int getItemCount() {
        Log.e("TITLE","result" + super.getItemCount());
        return super.getItemCount();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {

        private  TextView osIdItem;
        private  TextView osClientItem;
        private  TextView osDateItem;
        private  TextView osValueItem;

        public ViewHolder(View itemView) {
            super(itemView);

            osIdItem =  itemView.findViewById(R.id.osIDItem);
            osClientItem =   itemView.findViewById(R.id.osClientNameItem);
            osDateItem =  itemView.findViewById(R.id.osDateItem);
            osValueItem =  itemView.findViewById(R.id.osValueItem);
        }
    }
}

The commented part of my Activity shows me that I am getting both fields from my database, as an array, so I don't know why my RecyclerView doesn't.

1 Answers

The FirestoreRecyclerAdapter creates one child view in the RecyclerView for each document. Since you have only a single document under cliente, it is the expected behavior that only a single child shows up in the recycler view.

If you need a different behavior, you'll probably want to create your own adapter. The Android documentation on creating dynamic lists with RecyclerView contains a step-by-step guide for this.

Related