没有调用onBindViewHolder和FirebaseRecyclerAdapter的onCreateViewholder

时间:2018-03-13 12:21:20

标签: android firebase firebase-realtime-database android-recyclerview firebaseui

我正在尝试使用recyclerview从firebase获取详细信息来填充FirebaseRecyclerAdapter

活动代码如下: -

 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代码如下所示

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);
    }
}
}

模型类

    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;
    }
}

它转到FirebaseUserChatAdapter构造函数。但没有其他方法被调用。所以我的recyclerview没有填充。 有人可以帮忙吗。

2 个答案:

答案 0 :(得分:2)

您只需startListening方法中的onStart()

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

        mAdapter.startListening();
     }

并在onStop()

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

        mAdapter.stopListening();
     }

还要确保pojo中的变量与Firebase中数据库结构中的字段完全匹配。

看看文档

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

希望有所帮助

快乐编码

答案 1 :(得分:0)

在此之前:

   mRecyclerView.setAdapter(adapter);

添加:

   adapter.startListening();
希望它会起作用。