Recyclerview无法检索数据

时间:2018-04-28 12:53:50

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

RecyclerView messageList;
List<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter mAdapter;

 mAdapter = new MessageAdapter(messagesList);
    messageList = (RecyclerView) findViewById(R.id.messageList);
    linearLayoutManager = new LinearLayoutManager(this);
    messageList.setHasFixedSize(true);
    messageList.setLayoutManager(linearLayoutManager);
    messageList.setAdapter(mAdapter);

 private void fetchMessages() {

   rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Messages message = dataSnapshot.getValue(Messages.class);
            messagesList.add(message);
            mAdapter.notifyDataSetChanged();

            Log.d("Tag", String.valueOf(dataSnapshot.getValue(Messages.class)));
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

适配器

public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> messagesList;
private FirebaseAuth mAuth;

public MessageAdapter (List<Messages>messagesList)

{
    this.messagesList = messagesList;
}

public class MessageViewHolder extends RecyclerView.ViewHolder{
    public TextView messageText;
    public MessageViewHolder(View view)
    {
        super(view);
        messageText = (TextView)view.findViewById(R.id.message_text);
    }
}

@Override
public  int getItemCount()
{
    return messagesList.size();
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
    View V = LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_custom,parent,false);
    mAuth = FirebaseAuth.getInstance();
    return new MessageViewHolder(V);
}

@SuppressLint("ResourceType")
@Override
public void onBindViewHolder(MessageViewHolder holder, int position) {

        String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
        Messages messages = messagesList.get(position);
        String from_user = messages.getFrom();

//        if (from_user!=null && from_user.equals(current_user_id)){
//                holder.messageText.setBackgroundResource(R.drawable.text_background1);
//                holder.messageText.setTextColor(Color.red(R.color.red));
//            }else {
//                holder.messageText.setBackgroundResource(R.drawable.text_background2);
//                holder.messageText.setTextColor(Color.red(R.color.black));
//            }

        holder.messageText.setText(messages.getMessage());
    }
}

消息类

public class Messages {
private String from;
private String message;
private String type;
private boolean seen;
private long time;

public Messages(){};

public String getFrom() {
    return from;
}

public void setFrom(String from) {
    this.from = from;
}

public String getMessage() {
    return message;
}

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

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

public boolean isSeen() {
    return seen;
}

public void setSeen(boolean seen) {
    this.seen = seen;
}

public long getTime() {
    return time;
}

public void setTime(long time) {
    this.time = time;
}

}

当我调试时,我得到了这个输出

04-28 18:17:57.537 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@461a55
04-28 18:17:57.538 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@c9d4c6a
04-28 18:17:57.545 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@3e055b
04-28 18:17:57.546 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@f6c31f8
04-28 18:17:57.548 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@61b06d1
04-28 18:17:57.549 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@c3bc536
04-28 18:17:57.550 21546-21546/com.appmaster.akash.messageplus D/Tag: com.appmaster.akash.messageplus.Messages@94d9c37

屏幕截图https://ibb.co/e9ui8c

IDS

final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
MessageSenderId = mAuth.getCurrentUser().getUid();
MessageRecieverId = getIntent().getStringExtra("Recievers_Id");

在我有一个listview之前它很好......逻辑是正确的,方法也是如此,所以所有的数据都被提取并显示在listview中...然后我决定将它改为recyclerview和方法in recyclerview适配器已经更改,所以我不确定一切是否正确...请帮帮我

1 个答案:

答案 0 :(得分:0)

RecyclerView messageList;
ArrayList<Messages> messagesList = new ArrayList<>();
private LinearLayoutManager linearLayoutManager;
private MessageAdapter mAdapter;
private Messages messages;

mAdapter = new MessageAdapter(fetchMessage(),getMessages());
messageList = (RecyclerView) findViewById(R.id.messageList);
linearLayoutManager = new LinearLayoutManager(this);
messageList.setHasFixedSize(true);
messageList.setLayoutManager(linearLayoutManager);
messageList.setAdapter(mAdapter);

private ArrayList<Messages>fetchMessages() {rootRef.child("Messages").child(MessageSenderId).child(MessageRecieverId).addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {
         messages = dataSnapshot.getValue(Messages.class);
        messagesList.add(messages);
        mAdapter.notifyDataSetChanged();

        Log.d("Tag", String.valueOf(dataSnapshot.getValue(Messages.class)));
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});
return messgesList
}
 private Messages getMessage(){
 return messages;
}

然后在你的适配器

public class MessageAdapter extends 
RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> messagesList;
private FirebaseAuth mAuth;
private Messages messages;


public MessageAdapter (List<Messages>messagesList, Messsages messages)

{
this.messagesList = messagesList;
this.messages = messages;
}

public class MessageViewHolder extends RecyclerView.ViewHolder{
public TextView messageText;
public MessageViewHolder(View view)
{
    super(view);
    messageText = (TextView)view.findViewById(R.id.message_text);
}
}

@Override
public  int getItemCount()
{
return messagesList.size();
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType)
{
View V=LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_chat_custom, parent,false);
mAuth = FirebaseAuth.getInstance();
return new MessageViewHolder(V);
}

@SuppressLint("ResourceType")
@Override
public void onBindViewHolder(MessageViewHolder holder, int position) {

    String current_user_id = mAuth.getInstance().getCurrentUser().getUid();
    messages = messagesList.get(position);
    String from_user = messages.getFrom();

 //        if (from_user!=null && from_user.equals(current_user_id)){
//                
 holder.messageText.setBackgroundResource(R.drawable.text_background1);
 //                holder.messageText.setTextColor(Color.red(R.color.red));
 //            }else {
//                

holder.messageText.setBackgroundResource(R.drawable.text_background2);
  //                
 holder.messageText.setTextColor(Color.red(R.color.black));
 //            }

    holder.messageText.setText(messages.getMessage());
}
}