-1

I have the FirebaseRecyclerAdapter declaration below:

    private void updateList() {
        adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(
                User.class,
                R.layout.user_layout,
                ListOnlineViewHolder.class,
                counterRef
        ) {
            @Override
            protected void onBindViewHolder(@NonNull ListOnlineViewHolder holder, int position, @NonNull User model) {
                holder.txtEmail.setText((model.getEmail()));
            }

            @NonNull
            @Override
            public ListOnlineViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                return null;
            }
        };
        adapter.notifyDataSetChanged();
        listOnline.setAdapter(adapter);
    }

The declaration of adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder> gives an error of FirebaseRecyclerAdapter cannot be applied. I am aware that I need to build FirebaseRecyclerOptions. How do I build 2 options?

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
Chen
  • 860
  • 10
  • 32

1 Answers1

0

First, configure the adapter by building FirebaseRecyclerOptions.For example:

 FirebaseRecyclerOptions<User> options =
                new FirebaseRecyclerOptions.Builder<User>()
                        .setQuery(query, User.class)
                        .build();

Next create the FirebaseRecyclerAdapter object. You should already have a ViewHolder subclass for displaying each item. For example:

FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<User, ListOnlineViewHolder>(options) {
    @Override
    public ListOnlineViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        // Create a new instance of the ViewHolder, in this case we are using a custom
        // layout called R.layout.message for each item
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.message, parent, false);

        return new ListOnlineViewHolder(view);
    }

    @Override
    protected void onBindViewHolder(ListOnlineViewHolder holder, int position, User model) {

    }
};

Check here for more info:

https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md

Peter Haddad
  • 78,874
  • 25
  • 140
  • 134
  • what is the parent in this case? – Chen Mar 21 '20 at 17:42
  • Parent is of type viewgroup, you use it to call getContext() since the from method takes an instance of context https://stackoverflow.com/questions/10641144/difference-between-getcontext-getapplicationcontext-getbasecontext-and – Peter Haddad Mar 21 '20 at 17:52
  • have you checked the answer? – Peter Haddad Mar 22 '20 at 14:59
  • yes, I did. Thanks for the answer. But where do I put `ListOnlineViewHolder.class, counterRef` in the newer version of `FirebaseRecyclerAdapter`? – Chen Mar 22 '20 at 15:44
  • here `.setQuery(counterRef, User.class)` , regarding the `ListOnlineViewHolder.class` just copy the answer – Peter Haddad Mar 22 '20 at 15:46
  • @IsaakNewton Since the answer helped you, please upvote it and mark it as correct, thank you! – Peter Haddad Mar 23 '20 at 12:44