gpt4 book ai didi

android - 无法让 FirestoreRecyclerAdapter 显示项目

转载 作者:行者123 更新时间:2023-12-04 16:30:06 24 4
gpt4 key购买 nike

我正在更新我的应用程序以使用 Firebase 的 Firestore 数据库。我正在努力让应用程序显示从数据库中检索到的数据。数据检索正常,但未显示。通过设置断点,我已经确定 ViewHolder 在任何时候都没有绑定(bind)到适配器。

数据显示在 fragment 中。 fragment 布局是(我已经去掉了不相关的东西,如填充、大小等):

<android.support.constraint.ConstraintLayout
android:id="@+id/layout_charts_list"
tools:context="apppath.fragments.ChartListFragment">

<TextView
android:id="@+id/loading_list"
android:text="@string/loading_my_charts" />

<TextView
android:id="@+id/empty_list"
android:text="@string/my_charts_empty"
android:visibility="gone"/>

<android.support.v7.widget.RecyclerView
android:id="@+id/charts_list" />

</android.support.constraint.ConstraintLayout>

fragment 代码本身是:

public abstract class ChartListFragment extends Fragment {

private FirebaseFirestore mDatabaseRef;
private FirestoreRecyclerAdapter<Chart, ChartViewHolder> mAdapter;
private Query mChartsQuery;
private RecyclerView mRecycler;

public ChartListFragment() {}

@Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);

View rootView = inflater.inflate(
R.layout.fragment_charts_list, container, false);

mRecycler = rootView.findViewById(R.id.charts_list);
mRecycler.setHasFixedSize(true);

// Set up Layout Manager, and set Recycler View to use it
LinearLayoutManager mManager = new LinearLayoutManager(getActivity());
mManager.setReverseLayout(true);
mManager.setStackFromEnd(true);
mRecycler.setLayoutManager(mManager);

// Connect to the database, and get the appropriate query (as set in the actual fragment)
mDatabaseRef = FirebaseFirestore.getInstance();
mChartsQuery = getQuery(mDatabaseRef);

// Set up Recycler Adapter
FirestoreRecyclerOptions<Chart> recyclerOptions = new FirestoreRecyclerOptions.Builder<Chart>()
.setQuery(mChartsQuery, Chart.class)
.build();

mAdapter = new ChartListAdapter(recyclerOptions);

// Use Recycler Adapter in RecyclerView
mRecycler.setAdapter(mAdapter);

return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

// Add listener to charts collection, and deal with any changes by re-showing the list
mChartsQuery.addSnapshotListener(new EventListener<QuerySnapshot>() {
@Override
public void onEvent(@Nullable QuerySnapshot queryDocumentSnapshots,
@Nullable FirebaseFirestoreException e) {

if (queryDocumentSnapshots != null && queryDocumentSnapshots.isEmpty()) {
((MainActivity) getActivity()).setPage(1);
mRecycler.setVisibility(View.GONE);
}else {
mRecycler.setVisibility(View.VISIBLE);
}

}
});
}


// HELPER FUNCTIONS

public abstract Query getQuery(FirebaseFirestore databaseReference);

}

ChartListAdapter如下:

public class ChartListAdapter
extends FirestoreRecyclerAdapter<Chart, ChartViewHolder> {

public ChartListAdapter(FirestoreRecyclerOptions recyclerOptions) {
super(recyclerOptions);

}

@Override
protected void onBindViewHolder(ChartViewHolder holder, int position, Chart model) {
holder.setChartName(model.getName());

// Bind Chart to ViewHolder
holder.bindToChart(model);
}

@Override
public ChartViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chart, parent, false);

return new ChartViewHolder(view);
}
}

图 TableView 持有者:

public class ChartViewHolder extends RecyclerView.ViewHolder {

private TextView chartNameView;
private String chartKey;

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

chartNameView = itemView.findViewById(R.id.chart_name);
}

public void setChartName(String chartName) {
chartNameView.setText(chartName);
}

public void bindToChart(Chart chart) {
chartKey = chart.getKey();
chartNameView.setText(chart.getName());
}

public String getChartKey() {
return chartKey;
}
}

调用了 ChartListAdapter 构造函数,但从未调用过 onBindViewHolder 和 onCreateViewHolder,也从未访问过 ChartViewHolder。我错过了一行代码吗?或者这样做完全错误?我对 Adapters 和 RecyclerViews 不是很熟悉,所以我发现很难掌握将它们放在一起。

最佳答案

对于那些从 Google 来到这里的人,请在选项上设置生命周期所有者,以便自动调用开始/停止监听。

FirestoreRecyclerOptions<Chart> recyclerOptions = FirestoreRecyclerOptions.Builder<Chart>()
.setQuery(mChartsQuery, Chart.class)
.setLifecycleOwner(this)
.build();

关于android - 无法让 FirestoreRecyclerAdapter 显示项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50119627/

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