我正在使用nkzawa socket-io库处理聊天应用。我已成功将消息发送到服务器并在我的日志cat中检索它们。我无法更新ui,因为适配器中没有完成get message事件。
我希望能够将基于uid的消息与我或其他人分开并正确更新ui。到目前为止,如果我在适配器或主UI中检索消息,我是否对如何使用适配器感到困惑?
这是我的适配器代码:
public class MessagesAdapter extends RecyclerView.Adapter<MessagesViewHolder> {
private static final String TAG = MessagesAdapter.class.getSimpleName();
private Context context;
private DBHelper helper;
private TingTingUser user;
private List<HomeMessage> messageList;
public MessagesAdapter(Context context, List<HomeMessage> messageList) {
this.context = context;
this.messageList = messageList;
helper = new DBHelper(context);
}
@Override
public MessagesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View msgView = LayoutInflater.from(parent.getContext()).inflate(R.layout.messages_layout, parent, false);
return new MessagesViewHolder(msgView);
}
@Override
public void onBindViewHolder(MessagesViewHolder holder, int position) {
HomeMessage message = messageList.get(position);
//holder.upDateUI();
}
@Override
public int getItemCount() {
if (messageList.size() == 0) {
return 0;
}
return messageList.size();
}
public void upDateUI(String usersId) {
user = helper.getCurrentUser(1);
String currUid = user.getUserId();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.messages_layout, null, false);
MessagesViewHolder holder = new MessagesViewHolder(view);
Log.d(TAG, "Uid form DB is:\t" + currUid);
if (usersId != currUid) {
holder.mainLayout.setGravity(Gravity.LEFT);
holder.nameLayout.setVisibility(View.VISIBLE);
holder.msgHolderLayout.setBackgroundResource(R.drawable.receiver_msg_bkg);
} else {
holder.mainLayout.setGravity(Gravity.RIGHT);
holder.nameLayout.setVisibility(View.GONE);
holder.msgHolderLayout.setBackgroundResource(R.drawable.sender_msg_bkg);
}
} }
这是我检索所有套接字消息的方法:
private void getPublicMessages(){
String get_event = "cc-" + chatRoomId;
Log.d(TAG, "Get Event Name is:\t " + get_event.toString());
mSocket.on(get_event, new Emitter.Listener() {
@Override
public void call(final Object... args) {
runOnUiThread(new Runnable() {
@Override
public void run() {
try {
JSONObject jsonObject = (JSONObject) args[0];
//JSONObject dataObject = jsonObject.getJSONObject();
String type = jsonObject.getString("type");
Log.d(TAG, "Message is of type:\t" + type);
JSONObject contentObject = jsonObject.getJSONObject("content");
String userId = contentObject.getString("userId");
String roomId = contentObject.getString("roomId");
String message = contentObject.getString("message");
String name = contentObject.getString("name");
String avatar = contentObject.getString("avatar");
Log.d(TAG, "Get Message is:\t" + message);
Log.d(TAG, "roomId:\t" + roomId);
Log.d(TAG, "Get userId is:\t" + userId);
Log.d(TAG, "Get name is:\t" + name);
Log.d(TAG, "Get avatar is:\t" + avatar);
TingTingUser otherUsers = new TingTingUser(userId, "Abhiram Labhani", "Unknown");
HomeMessage othersHomeMessages = new HomeMessage(message, otherUsers, System.currentTimeMillis());
List<HomeMessage> othersList = new ArrayList<>();
othersList.add(othersHomeMessages);
Log.d(TAG, "Other Messages size is:\t " + othersList.size());
messageList.add(othersHomeMessages);
messagesAdapter.notifyDataSetChanged();
Log.d(TAG, "All Messages list size is:\t " + messageList.size());
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
});
}
名称,消息等值直接来自活动。我应该如何更新ui,这个适配器代码是否可行,或者我应该将此方法放在适配器中并相应地更新? 谢谢大家。