FirebaseUI中的适配器为空

时间:2019-07-04 22:06:37

标签: android android-recyclerview recycler-adapter firebaseui

我使用了Firebase Recycler Adapter来获取数据。但总是会出错,

  

尝试调用虚拟方法'void   androidx.recyclerview.widget.RecyclerView.setAdapter(androidx.recyclerview.widget.RecyclerView $ Adapter)'   在空对象引用上

我的片段是

public class MyBooksFragment extends BaseFragment {


    private ShimmerFrameLayout mShimmerViewContainer;
    private RecyclerView recyclerView ;
    private FirebaseRecyclerAdapter adapter;
    private FirebaseUser user= FirebaseAuth.getInstance().getCurrentUser();

    @Override
    public BaseFragment provideFragment() {

        return new MyBooksFragment();
    }

    @Override
    public View provideFragmentView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_my_books,parent,false);

        mShimmerViewContainer=view.findViewById(R.id.mybook_shimmer_view_container);

        RecyclerView recyclerView = view.findViewById(R.id.recycler_view_books);
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

        fetchBooks(1);


        FloatingActionButton fab = view.findViewById(R.id.addBookFab_);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), BookAdder.class);
                startActivity(intent);
            }
        });

        return view;

    }


    private FirebaseRecyclerOptions<Book> queryBuilderParser(){
        Query bookQuery= FirebaseDatabase
                .getInstance()
                .getReference()
                .child("Books")
                .orderByChild("ownerID")
                .equalTo(user.getUid());

        FirebaseRecyclerOptions<Book> options =
                new FirebaseRecyclerOptions.Builder<Book>()
                        .setQuery(bookQuery,Book.class)
                        .build();
        return options;
    }


    protected void fetchBooks(int type)
    {
        Log.e("profile",type+" ");

        FirebaseRecyclerOptions<Book> options;

                Query bookQuery= FirebaseDatabase
                        .getInstance()
                        .getReference()
                        .child("Books")
                        .orderByChild("ownerID")
                        .equalTo(user.getUid());

                options = new FirebaseRecyclerOptions.Builder<Book>()
                        .setQuery(bookQuery,Book.class)
                        .build();



        Log.e("profile","inside fetchReviw "+options.getSnapshots());

        adapter= new FirebaseRecyclerAdapter<Book, BookViewHolder>(options) {
            @NonNull
            @Override
            public BookViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                View view = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.book_row_item, parent, false);

                return new BookViewHolder(view);
            }

            @Override
            protected void onBindViewHolder(@NonNull BookViewHolder bookViewHolder, int i, @NonNull Book book) {


                bookViewHolder.book_name.setText(book.getBookName());
                bookViewHolder.book_author.setText(book.getAuthors());
                bookViewHolder.book_category.setText(book.getCategory());
                bookViewHolder.book_rating.setText(book.getRating()+"");
                Glide.with(getActivity()).load(book.getThumbnail()).into(bookViewHolder.img_thumbnail);

            }

            @Override
            public void onDataChanged() {


                if(getItemCount()>0 && mShimmerViewContainer!=null){
                    mShimmerViewContainer.stopShimmerAnimation();
                    mShimmerViewContainer.setVisibility(View.GONE);
                }
            }



            @Override
            public void onError(DatabaseError e) {
                // Called when there is an error getting data. You may want to update
                // your UI to display an error message to the user.
                // ...
                Log.e("profile","database error");
            }



        };

        recyclerView.setAdapter(adapter);


    }


}

在这里,我扩展了一个基本片段,该片段扩展了片段

1 个答案:

答案 0 :(得分:0)

您尚未在适配器中输入正确的参数。适配器在以下位置接受4个参数:

  • 对象类
  • 列出项目布局资源
  • 观看者类
  • 查询/ firebase参考

这是它的外观,适配器也做了很多硬道理,因此您只需要使用populateViewHolder函数:

FirebaseRecyclerViewAdapter<Object, ObjectViewHolder> adapter = new FirebaseRecyclerViewAdapter<Object, ObjectViewHolder>
(Object.class, android.R.layout.*list_item_layout*, ObjectViewHolder.class, objectQuery) {
     public void populateViewHolder(ObjectViewHolder ObjectViewHolder, Object Object) {

        //in your case the following lines:
            bookViewHolder.book_name.setText(book.getBookName());
            bookViewHolder.book_author.setText(book.getAuthors());
            bookViewHolder.book_category.setText(book.getCategory());
            bookViewHolder.book_rating.setText(book.getRating()+"");
            Glide.with(getActivity()).load(book.getThumbnail()).into(bookViewHolder.img_thumbnail);

     }
 };

 recycler.setAdapter(mAdapter);