0

I am trying to populate a recyclerview using FirebaseRecyclerAdapter fetching details from firebase.

Activity code is given below :-

 private FirebaseRecyclerAdapter<UserChat,RecyclerView.ViewHolder> adapter; 

final FirebaseRecyclerOptions<UserChat> options = new FirebaseRecyclerOptions.Builder<UserChat>()
                .setQuery(queryforDisplayMessages, UserChat.class)
                .setLifecycleOwner(this)
                .build();

adapter=new FirebaseUserChatAdapter(options);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getBaseContext()));
    mRecyclerView.setAdapter(adapter);

FirebaseAdapter Code is given below

public class FirebaseUserChatAdapter extends FirebaseRecyclerAdapter<UserChat,RecyclerView.ViewHolder> {
private static final String TAG=FirebaseUserChatAdapter.class.getSimpleName();

private static final int VIEW_TYPE_MESSAGE_SENT = 1;
private static final int VIEW_TYPE_MESSAGE_RECEIVED = 2;

/**
 * Initialize a {@link RecyclerView.Adapter} that listens to a Firebase query. See
 * {@link FirebaseRecyclerOptions} for configuration options.
 *
 * @param options
 */
public FirebaseUserChatAdapter(@NonNull FirebaseRecyclerOptions<UserChat> options) {
    super(options);
}

@Override
public int getItemViewType(int position) {
    UserChat message = getItem(position);

    Log.d(TAG,"usermodel"+message.getUserModel().getId());

    if (message.getUserModel().getId().equals("GgGwv7BzOSSoMA5ve2047Neg8aC3")) {
        // If the current user is the sender of the message
        return VIEW_TYPE_MESSAGE_SENT;
    } else {
        // If some other user sent the message
        return VIEW_TYPE_MESSAGE_RECEIVED;
    }
}

@Override
protected void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position, @NonNull UserChat model) {

    UserChat message = getItem(position);



    switch (holder.getItemViewType()){

        case VIEW_TYPE_MESSAGE_SENT:

            ((FirebaseUserChatViewHolderSent)holder).tvSent.setText(model.getMessage());

        case VIEW_TYPE_MESSAGE_RECEIVED:

            ((FirebaseUserChatViewHolderReceived)holder).tvReceived.setText(model.getMessage());
    }

}

@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View view;

    switch (viewType){

        case VIEW_TYPE_MESSAGE_SENT:

            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.chat_user1_item, parent, false);
            return new FirebaseUserChatViewHolderSent(view);

        case VIEW_TYPE_MESSAGE_RECEIVED:
            view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.chat_user2_item, parent, false);
            return new FirebaseUserChatViewHolderReceived(view);
    }

    return null;
}

class FirebaseUserChatViewHolderSent extends RecyclerView.ViewHolder {

    TextView tvSent;

    public FirebaseUserChatViewHolderSent(View itemView) {
        super(itemView);
        tvSent=itemView.findViewById(R.id.textview_messagesent);
    }
}

class FirebaseUserChatViewHolderReceived extends RecyclerView.ViewHolder {

    TextView tvReceived;

    public FirebaseUserChatViewHolderReceived(View itemView) {
        super(itemView);
        tvReceived=itemView.findViewById(R.id.textview_messagereceived);
    }
}
}

Model class

    package com.iworld.daxi.Model;


    import java.security.Timestamp;

    /**
     * Created by Admin on 20/02/2018.

 */

public class UserChat {


    private boolean datetimestamp;
    private String message;
    private String status;
    private Object timeStamp;
    private UserModel userModel;

    public UserChat(){

    }

    public UserChat(boolean datetimestamp, String message, String status, Object timeStamp, UserModel userModel) {
        this.datetimestamp = datetimestamp;
        this.message = message;
        this.status = status;
        this.timeStamp = timeStamp;
        this.userModel = userModel;
    }

    public boolean isDatetimestamp() {
        return datetimestamp;
    }

    public void setDatetimestamp(boolean datetimestamp) {
        this.datetimestamp = datetimestamp;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public String getStatus() {
        return status;
    }

    public void setStatus(String status) {
        this.status = status;
    }

    public Object getTimeStamp() {
        return timeStamp;
    }

    public void setTimeStamp(Object timeStamp) {
        this.timeStamp = timeStamp;
    }

    public UserModel getUserModel() {
        return userModel;
    }

    public void setUserModel(UserModel userModel) {
        this.userModel = userModel;
    }
}

Its going to the FirebaseUserChatAdapter constructor. But no other methods are getting called. And so my recyclerview is not getting populated. Can someone help please.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
neab
  • 91
  • 1
  • 3
  • 10
  • can you post your pojo class please , and a photo of your database structure in firebase – Gastón Saillén Mar 13 '18 at 12:25
  • 1
    Override this method and return number of items "getItemCount()". – RajaReddy PolamReddy Mar 13 '18 at 12:28
  • @GastonSaillen Pojo class added. It was working fine when I used ListView Adapter . But i want to use RecyclerAdaper since there are different layouts. But those methods are not getting called. – neab Mar 13 '18 at 12:34
  • your pojo need to match exactly your database items, can you post a photo of your firebase database ? just the name of the fields – Gastón Saillén Mar 13 '18 at 12:35
  • it exactly matches the database items . Beacuse it was working fine when i used FirebaseListAdapter @GastonSaillen – neab Mar 13 '18 at 12:39

2 Answers2

3

you just need to startListening in your onStart() method

 protected void onStart() {
         super.onStart();

        mAdapter.startListening();
     }

and in your onStop()

 protected void onStop() {
         super.onStop();

        mAdapter.stopListening();
     }

Also make sure your variables in the pojo match exactly with the fields in your datbase structure in Firebase.

Take a look at the doc

https://github.com/firebase/FirebaseUI-Android/tree/master/firestore/#firestorerecycleradapter-lifecycle

hope it helps

Happy coding

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
  • I think the doc url should be : https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#firebaserecycleradapter-lifecycle – Anatol Bivol Apr 18 '21 at 17:00
2

Before this:

   mRecyclerView.setAdapter(adapter);

add this:

   adapter.startListening();

hopefully it will work.

Hasan Bou Taam
  • 4,017
  • 2
  • 12
  • 22