-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_java_com_example_harsh_freechat_MessageAdapter.java
118 lines (79 loc) · 3.46 KB
/
main_java_com_example_harsh_freechat_MessageAdapter.java
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
117
118
package com.example.harsh.freechat;
import android.graphics.Color;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.squareup.picasso.Picasso;
import java.util.List;
import de.hdodenhof.circleimageview.CircleImageView;
/**
* Created by harsh on 01-01-2018.
*/
public class MessageAdapter extends RecyclerView.Adapter<MessageAdapter.MessageViewHolder>{
private List<Messages> mMessageList;
private FirebaseAuth mAuth;
private DatabaseReference mUserDatabase;
public MessageAdapter(List<Messages> mMessageList) {
this.mMessageList = mMessageList;
}
@Override
public MessageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.message_single_layout ,parent, false);
return new MessageViewHolder(v);
}
public class MessageViewHolder extends RecyclerView.ViewHolder {
public TextView messageText;
public CircleImageView profileImage;
public TextView displayName;
public ImageView messageImage;
public MessageViewHolder(View view) {
super(view);
messageText = (TextView) view.findViewById(R.id.message_text_layout);
profileImage = (CircleImageView) view.findViewById(R.id.message_profile_layout);
displayName = (TextView) view.findViewById(R.id.name_text_layout);
messageImage = (ImageView) view.findViewById(R.id.message_image_layout);
}
}
@Override
public void onBindViewHolder(final MessageViewHolder viewHolder, int i) {
Messages c = mMessageList.get(i);
String from_user = c.getFrom();
String message_type = c.getType();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(from_user);
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("thumb_image").getValue().toString();
viewHolder.displayName.setText(name);
Picasso.with(viewHolder.profileImage.getContext()).load(image)
.placeholder(R.drawable.avatar).into(viewHolder.profileImage);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
if(message_type.equals("text")) {
viewHolder.messageText.setText(c.getMessage());
viewHolder.messageImage.setVisibility(View.INVISIBLE);
} else {
viewHolder.messageText.setVisibility(View.INVISIBLE);
Picasso.with(viewHolder.profileImage.getContext()).load(c.getMessage())
.placeholder(R.drawable.avatar).into(viewHolder.messageImage);
}
}
@Override
public int getItemCount() {
return mMessageList.size();
}
}