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
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.