我试图添加&在回收站视图中动态显示消息,但我无法弄清楚如何将适当的数据类型发送到它的适配器。在“getNewMessage(dataSnapshot)”函数中,我必须将所有消息添加到列表中,并通过适配器将其发送到回收器视图。我试过了两个
MessageModel model = dataSnapshot.getValue(MessageModel.class)
并创建一个包含List<MessageModel> list
的SampleModel,和
SampleModel model = dataSnapshot.getValue(SampleModel.class);
他们没有工作。这里是使用发送消息创建的示例JSON文件:
"chat" : {
"-Ksbjn0yCEB6EXhNNCM5" : {
"author" : "Ali Alacan",
"content" : "dummy content",
"date" : "Mon Aug 28 10:29:50 GMT+03:00 2017",
"id" : "H6huNPUggjtugjsERPCRSAp1"
},
"-KsbjpUtjp0oeipjjxMI" : {
"author" : "Ali Alacan",
"content" : "dummy content",
"date" : "Mon Aug 28 10:30:00 GMT+03:00 2017",
"id" : "H6huNPUggjtugjsERPCRSAp1"
}
我正在使用以下代码添加新消息,我从firebase文档中学到了这些消息。
@Exclude
public Map<String, Object> toMap(MessageModel messageModel) {
HashMap<String, Object> result = new HashMap<>();
result.put("id", messageModel.getId());
result.put("author", messageModel.getAuthor());
result.put("content", messageModel.getContent());
result.put("date", messageModel.getDate());
return result;
}
private void sendMessage() {
if (!TextUtils.isEmpty(etMessage.getText())) {
Date currentTime = Calendar.getInstance().getTime();
String key = firebaseDatabase.push().getKey();
MessageModel message = new MessageModel(UserData.getInstance().getUserId(), UserData.getInstance().getName(), "dummy content", currentTime.toString());
Map<String, Object> messageValues = toMap(message);
Map<String, Object> childUpdates = new HashMap<>();
childUpdates.put("/chat/" + key, messageValues);
childUpdates.put("/user-chat/" + UserData.getInstance().getUserId() + "/" + key, messageValues);
firebaseDatabase.updateChildren(childUpdates);
} else {
Snackbar snackbar = Snackbar
.make(getActivity().findViewById(R.id.dashboard_container), "Enter a message please ! " + UserData.getInstance().getName(), Snackbar.LENGTH_LONG);
snackbar.show();
}
}
我的MessageModel是:
public class MessageModel {
String id;
String author;
String content;
String date;
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public MessageModel(String id, String author, String content, String date) {
this.id = id;
this.author = author;
this.content = content;
this.date = date;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public MessageModel() {
}
}
和firebase监听器
firebaseDatabase.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
getNewMessage(dataSnapshot);
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
// getNewMessage(dataSnapshot);
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
// taskDeletion(dataSnapshot);
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
RecyclerView适配器:
public class MyMessageRecyclerViewAdapter extends RecyclerView.Adapter<MyMessageRecyclerViewAdapter.ViewHolder> {
private final List<MessageModel> mValues;
private final OnListFragmentInteractionListener mListener;
public MyMessageRecyclerViewAdapter(List<MessageModel> items, OnListFragmentInteractionListener listener) {
mValues = items;
mListener = listener;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_chat, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.mItem = mValues.get(position);
holder.mIdView.setText(mValues.get(position).getId());
holder.mContentView.setText(mValues.get(position).getContent());
holder.mAuthor.setText(mValues.get(position).getAuthor());
holder.mDate.setText(mValues.get(position).getDate());
holder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (null != mListener) {
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mListener.onListFragmentInteraction(holder.mItem);
}
}
});
}
@Override
public int getItemCount() {
return mValues.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
public final View mView;
public final TextView mIdView;
public final TextView mContentView;
TextView mAuthor;
TextView mDate;
public MessageModel mItem;
public ViewHolder(View view) {
super(view);
mView = view;
mIdView = (TextView) view.findViewById(R.id.id);
mContentView = (TextView) view.findViewById(R.id.content);
mAuthor = (TextView) view.findViewById(R.id.author);
mDate = (TextView) view.findViewById(R.id.date);
}
@Override
public String toString() {
return super.toString() + " '" + mContentView.getText() + "'";
}
}
很抱歉阅读这么长的帖子,谢谢你的时间。
答案 0 :(得分:0)
您不再需要使用toMap,因为您已经拥有POJO,这会让您对自己所做的事情感到更加困惑。
在您的活动中创建一个列表。
ArrayList<MessageModel> myListModel = new ArrayList<MessageModel>();
在你的firebase侦听器上执行此操作。列出您的活动
这是为儿童添加的。
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
MessageModel tempMessage = dataSnapshot.getValue(MessageModel.class);
myListModel.add(tempRace);
}
现在你有了一个列表模型。通过使用Firebase的onChildAdeed。 使用它来提供适配器型号并应用它。
火炬的重要性。在火灾基地的每个查询中,如果你需要做任何其他事情之后的事情,那么:&#34; onChildAdded&#34;在您的情况下,您需要向THE SAME DatabaseReference添加另一个侦听器。我不知道,你甚至没有将你的活动包括在你的DatabaseReference中,所以我会提供一个。
DatabaseReference myReference = firebaseDatabase.getReferane("chat");
myReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
MyMessageRecyclerAdapter myMessageAdapter= new MyMessageRecyclerAdapter(myListModel, listener);
//now you have to apply this to the RecyclerView.
yourWhateverNameofTheRecyclerview.SetAdapter(myMessageAdater);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});