2

I'm following this tutorial, according to which when I start my app I should be able to see the items (documents) which are already added to Firestore. But this is happening

enter image description here

The onCreateViewHolder is called only when I tap on textview, and when out of focus, the view is refreshed. I don't understand what is going on. I tested my app on Browserstack, and getting the same issue with other devices too.

Here is my code:

ScrollingActivity.java

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_scrolling);
    Log.d(TAG, "onCreate: started");

    RecyclerView recyclerView = findViewById(R.id.expense_list);

    FirebaseFirestore db = FirebaseFirestore.getInstance();
    mAuth = FirebaseAuth.getInstance();
    Query query = db.collection("users").document(mAuth.getCurrentUser().getUid()).collection("expenses");

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

    adapter = new FirestoreRecyclerAdapter<Expense, ExpenseViewHolder>(options) {
        @NonNull
        @Override
        public ExpenseViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            Log.d(TAG, "onCreateViewHolder: called");
            LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
            View view = layoutInflater.inflate(R.layout.individual_expenditure_layout, parent, false);
            return new ExpenseViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull ExpenseViewHolder expenseViewHolder, int position, @NonNull Expense expense) {
            Log.d(TAG, "onBindViewHolder: called");
            expenseViewHolder.setAmount(expense.getAmount());
            expenseViewHolder.setDescription(expense.getDescription());
        }
    };

    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);
}

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


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

Expense.java

public class Expense {

    private Timestamp timestamp;
    private float amount;
    private String description;

    public Expense() { timestamp = Timestamp.now(); }

    public Expense(float amount, String description){
        this.timestamp = Timestamp.now();
        this.amount = amount;
        this.description = description;
    }

    public float getAmount() {
        return amount;
    }

    public String getDescription() {
        return description;
    }
}

ExpenseViewHolder.java

public class ExpenseViewHolder extends RecyclerView.ViewHolder {

    private TextView textView_amount, textView_description;

    public ExpenseViewHolder(@NonNull View itemView) {
        super(itemView);

        textView_amount = itemView.findViewById(R.id.textView_Amount);
        textView_description = itemView.findViewById(R.id.textView_Description);
    }

    public void setAmount(float amount) {
        textView_amount.setText(String.format(Locale.US,"₹ %.2f", amount));
    }

    public void setDescription(String description) {
        textView_description.setText(description);
    }
}

activity_scrolling.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

    <androidx.core.widget.NestedScrollView
        android:id="@+id/nestedScrollView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_anchorGravity="center"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:paddingBottom="130dp">
        <!--    TODO: This paddingBottom will not work for every device    -->

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/expense_list"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="com.google.android.material.appbar.AppBarLayout$ScrollingViewBehavior">

        </androidx.recyclerview.widget.RecyclerView>
    </androidx.core.widget.NestedScrollView>

    <include
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        layout="@layout/add_expense_form"
        android:layout_gravity="bottom"
        />


</androidx.coordinatorlayout.widget.CoordinatorLayout>

I have been stuck on this for 2 days now.

Didn't find these helpful: FirestoreRecyclerAdapter not updating views, RecyclerView not calling onCreateViewHolder or onBindView, Can't get FirestoreRecyclerAdapter to show items

Found this tutorial, they have done similar stuff only. Don't know why this happening with me.

Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44
  • 2
    It sounds like `query` doesn't give any results in that case. You could see what happens if you get the data for the query without FirebaseUI as shown here: https://firebase.google.com/docs/firestore/query-data/get-data#get_multiple_documents_from_a_collection So in your case you'd do `query.get()...` and then check if you get any results or errors there. – Frank van Puffelen Mar 12 '20 at 17:04
  • But the data appears as soon as tap on any `textview`, so the query is returning something. Just not when the app launches. – Nikhil Wagh Mar 13 '20 at 02:25
  • 1
    Interesting. Nothing in the code you shared seems likely to be causing that. Note that the animation is not very insightful to me, as I have no idea what the app is supposed to be doing there. If you can reproduce the problem with just logging output, it's typically much easier to help. – Frank van Puffelen Mar 13 '20 at 03:46

1 Answers1

2

The problem is these lines:

recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter); 

It has to be updated to:

recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));

setHasFixedSize was causing it to not get updated.

Nikhil Wagh
  • 1,376
  • 1
  • 24
  • 44