我正在开发简单的项目。 CategoryFragment是MainActivity中第一个添加的片段。我希望从FireBase接收数据并使用RecyclerView显示它。
在我的代码下面。该应用程序在启动期间不会下降。没有任何错误。 我想我应该为我的目的使用一些方法,但我在Firebase中是新手。
public class CategoryFragment extends Fragment {
public DatabaseReference mRootDB;
private RecyclerView mRecyclerView;
public FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRootDB = FirebaseDatabase.getInstance().getReference().child("categories");
adapter = new FirebaseRecyclerAdapter <Category, CategoryViewHolder>(
Category.class,
R.layout.model_category,
CategoryViewHolder.class,
mRootDB)
{
@Override
protected void populateViewHolder(CategoryViewHolder viewHolder, Category model, int position) {
viewHolder.setCategory(model.getName());
}
};
mRecyclerView.setAdapter(adapter);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_category, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_RV);
return rootView;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView category_CV;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
category_CV = (TextView) mView.findViewById(R.id.category_CV);
}
public void setCategory(String data){
category_CV.setText(data);
}
}
}
答案 0 :(得分:1)
如果我是你,我不会覆盖onActivityCreated(),你可以在onCreateView()中创建并返回RecyclerView。您确定Firebase中有数据吗?您可以像这样覆盖parseSnapshot()并且可以设置一个断点以确保某些东西回来,如果确实如此,那么您可能需要检查您的RecyclerView自定义布局。
public class CategoryFragment extends Fragment {
public DatabaseReference mRootDB;
private RecyclerView mRecyclerView;
public FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_category, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_RV);
mRootDB = FirebaseDatabase.getInstance().getReference().child("categories");
adapter = new FirebaseRecyclerAdapter <Category, CategoryViewHolder>(
Category.class,
R.layout.model_category,
CategoryViewHolder.class,
mRootDB)
{
@Override
protected Category parseSnapshot(DataSnapshot snapshot) {
Category category = super.parseSnapshot(snapshot);
if (category != null){
category.setId(snapshot.getKey());
}
return category;
}
@Override
protected void populateViewHolder(CategoryViewHolder viewHolder, Category model, int position) {
viewHolder.setCategory(model.getName());
}
};
mRecyclerView.setAdapter(adapter);
return rootView;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView category_CV;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
category_CV = (TextView) mView.findViewById(R.id.category_CV);
}
public void setCategory(String data){
category_CV.setText(data);
}
}
}