-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAdapterChatList.java
More file actions
116 lines (95 loc) · 3.83 KB
/
Copy pathAdapterChatList.java
File metadata and controls
116 lines (95 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package com.codewithharry.firebase.adapters;
import android.content.Context;
import android.content.Intent;
import android.media.Image;
import android.provider.ContactsContract;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.codewithharry.firebase.ChatActivity;
import com.codewithharry.firebase.R;
import com.codewithharry.firebase.models.ModelUser;
import com.squareup.picasso.Picasso;
import org.w3c.dom.Text;
import java.util.HashMap;
import java.util.List;
public class AdapterChatList extends RecyclerView.Adapter<AdapterChatList.myHolder> {
Context context;
List<ModelUser> userList;
private HashMap<String, String> lastMessageMap;
public AdapterChatList(Context context, List<ModelUser> userList) {
this.context = context;
this.userList = userList;
lastMessageMap = lastMessageMap;
}
@NonNull
@Override
public myHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
//inflate layout row_chatList.xml
View view = LayoutInflater.from(context).inflate(R.layout.row_chatlist, parent, false);
return new myHolder(view);
}
@Override
public void onBindViewHolder(@NonNull myHolder holder, int i) {
//get data
String hisUid = userList.get(i).getUid();
String userImage = userList.get(i).getImage();
String userName = userList.get(i).getName();
String lastMessage = lastMessageMap.get(hisUid);
//set data
holder.nameTv.setText(userName);
if (lastMessage == null || lastMessage.equals("default")) {
holder.lastMessageTv.setVisibility(View.GONE);
} else {
holder.lastMessageTv.setVisibility(View.VISIBLE);
holder.lastMessageTv.setText(lastMessage);
}
try {
Picasso.get().load(userImage).placeholder(R.drawable.ic_default_image_blue).into(holder.profileIv);
} catch (Exception e) {
Picasso.get().load(R.drawable.ic_default_image_blue).into(holder.profileIv);
}
//set online status of other users in chatlist
if (userList.get(i).getOnlineStatus().equals("Online")){
//online
holder.onlineStatusIv.setImageResource(R.drawable.circle_online);
} else {
//offline
holder.onlineStatusIv.setImageResource(R.drawable.circle_offline);
}
//handle click of user in chatlist
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//start chatActivity with that user
Intent intent = new Intent(context, ChatActivity.class);
intent.putExtra("hisUid", hisUid);
context.startActivity(intent);
}
});
}
public void setLastMessageMap(String userId, String lastMessage) {
lastMessageMap.put(userId, lastMessage);
}
@Override
public int getItemCount() {
return userList.size();
}
class myHolder extends RecyclerView.ViewHolder {
//views of row_chatList.xml
ImageView profileIv, onlineStatusIv;
TextView nameTv, lastMessageTv;
public myHolder(@NonNull View itemView) {
super(itemView);
//init views
profileIv = itemView.findViewById(R.id.profileIv);
onlineStatusIv = itemView.findViewById(R.id.onlineStatusIv);
nameTv = itemView.findViewById(R.id.nameTv);
lastMessageTv = itemView.findViewById(R.id.lastMessageTv);
}
}
}