gpt4 book ai didi

android - FirestoreRecyclerAdapter 不调用 onCreateViewHolder

转载 作者:行者123 更新时间:2023-12-04 10:25:04 26 4
gpt4 key购买 nike

我正在关注 this tutorial ,据此,当我启动我的应用程序时,我应该能够看到已添加到 Firestore 的项目(文档)。但这正在发生

enter image description here

onCreateViewHolder仅当我点击 textview 时才被调用 ,当失焦时, View 会刷新。
我不明白发生了什么。我在 Browserstack 上测试了我的应用程序,并且在其他设备上也遇到同样的问题。

这是我的代码:

滚动 Activity .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();
}

费用.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 滚动.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>

我已经坚持了2天了。

没有发现这些有帮助: FirestoreRecyclerAdapter not updating views , RecyclerView not calling onCreateViewHolder or onBindView , Can't get FirestoreRecyclerAdapter to show items

找到 this tutorial ,他们只做过类似的事情。不知道为什么这会发生在我身上。

最佳答案

问题是这些行:

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

它必须更新为:
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
setHasFixedSize导致它没有得到更新。

关于android - FirestoreRecyclerAdapter 不调用 onCreateViewHolder,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60658380/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com