FirebaseRecyclerAdapter未初始化

时间:2018-01-16 13:38:11

标签: android firebase firebase-realtime-database

好的,所以我想从我的数据库中检索数据,但我的file_get_contents没有初始化,通过debugget它只是跳过400方法。我错过了什么或者我做错了吗?以下是适配器所在的片段代码和RecyclerViewAdapter代码。

setUpAdapter

ViewHolder

ViewHolder

}

这是我想要从

中提取对象的数据库结构

enter image description here

GadgetObject代码

public class GadgetFragment extends Fragment {
private ImageView mImage;
private TextView mTitle,mDesc;
private FirebaseRecyclerAdapter<GadgetObject, GadgetViewHolder> mAdapter;
private DatabaseReference mReference;
private RecyclerView mRecycleView;
private FirebaseRecyclerOptions<GadgetObject> options;
private Query query;

public GadgetFragment() {
    query = FirebaseDatabase.getInstance().getReference().child("Gadgets");
    options = new FirebaseRecyclerOptions.Builder<GadgetObject>()
            .setQuery(query,GadgetObject.class).build();
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.gadget_fragment_layout,container,false);
    initScreen(v);
    return v;
}

private void initScreen(View view) {
    mRecycleView = (RecyclerView)view.findViewById(R.id.gadgetRecyclerView);
    mRecycleView.setHasFixedSize(true);
    mRecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
    setUpAdapter();
    mRecycleView.setAdapter(mAdapter);
}

private void setUpAdapter() {
    mAdapter = new FirebaseRecyclerAdapter<GadgetObject, GadgetViewHolder>(options) {

        @Override
        public GadgetViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getActivity())
                    .inflate(R.layout.gadget_recycler_grid_view,parent,false);
            return new GadgetViewHolder(view);
        }

        @Override
        protected void onBindViewHolder(@NonNull GadgetViewHolder holder, int position, @NonNull GadgetObject model) {
            holder.bindGadget(model);
            if(getActivity() != null) {
                Glide.with(getActivity()).load(model.getImageUrl()).into(mImage);
            } else {
                Toast.makeText(getActivity(), "Activity is null", Toast.LENGTH_SHORT).show();
            }
        }

    };


}

@Override
public void onStart() {
    super.onStart();
    mAdapter.startListening();
}

@Override
public void onStop() {
    super.onStop();
    mAdapter.stopListening();
}

1 个答案:

答案 0 :(得分:0)

尝试将代码从GadgetFragment构造函数移动到setupAdapter方法,如果这对您没有帮助,那么您应该提供更多类似模型类的代码并记录错误