Guest User

Untitled

a guest
Jun 20th, 2018
416
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. public class ChatActivity extends AppCompatActivity {
  2. DatabaseReference ref,ref1;
  3. TextView t;
  4. EditText e;
  5. ImageButton b;
  6. String contact,user;
  7. ArrayList<Chat> chatList;
  8. ListView listView;
  9. ChatAdapter chatAdapter;
  10. String msg;
  11. @Override
  12. protected void onCreate(Bundle savedInstanceState) {
  13. super.onCreate(savedInstanceState);
  14. setContentView(R.layout.activity_chat);
  15. chatList = new ArrayList<>();
  16. Intent i = getIntent();
  17. contact = i.getStringExtra("contact");
  18. user = i.getStringExtra("user");
  19. t = findViewById(R.id.textView3);
  20. e = findViewById(R.id.editText8);
  21. b = findViewById(R.id.imageButton);
  22. listView = findViewById(R.id.listView2);
  23. t.setText(" " + contact);
  24. ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
  25. ref.addChildEventListener(new ChildEventListener() {
  26. @Override
  27. public void onChildAdded(DataSnapshot dataSnapshot, String s) {
  28. if (!dataSnapshot.exists()) {
  29. return;
  30. }
  31. Toast.makeText(getApplicationContext(), "onChildAdded of ref", Toast.LENGTH_LONG).show();
  32. while(!(dataSnapshot.hasChild("type") && dataSnapshot.hasChild("msg") && dataSnapshot.hasChild("time"))){}
  33. chatList.add(new Chat(dataSnapshot.child("type").getValue(Integer.class), dataSnapshot.child("msg").getValue(String.class), dataSnapshot.child("time").getValue(Long.class)));
  34. if(chatAdapter!=null)
  35. chatAdapter.notifyDataSetChanged();
  36. }
  37.  
  38. @Override
  39. public void onChildChanged(DataSnapshot dataSnapshot, String s) {
  40.  
  41. }
  42.  
  43. @Override
  44. public void onChildRemoved(DataSnapshot dataSnapshot) {
  45.  
  46. }
  47.  
  48. @Override
  49. public void onChildMoved(DataSnapshot dataSnapshot, String s) {
  50.  
  51. }
  52.  
  53. @Override
  54. public void onCancelled(DatabaseError databaseError) {
  55.  
  56. }
  57. });
  58.  
  59. b.setOnClickListener(new View.OnClickListener() {
  60. @Override
  61. public void onClick(View v) {
  62. msg = e.getText().toString();
  63. ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
  64. ref1 = FirebaseDatabase.getInstance().getReference().child("Messages").child(contact).child(user);
  65. ref = ref.push();
  66. ref1 = ref1.push();
  67. ref.child("type").setValue(1);
  68. ref1.child("type").setValue(2);
  69. ref.child("msg").setValue(msg);
  70. ref1.child("msg").setValue(msg);
  71. ref.child("time").setValue(ServerValue.TIMESTAMP);
  72. ref1.child("time").setValue(ServerValue.TIMESTAMP);
  73. }
  74. });
  75. chatAdapter = new ChatAdapter(this, chatList);
  76. listView.setAdapter(chatAdapter);
  77. }
  78. }
  79.  
  80. public class ContactAdapter extends ArrayAdapter<Contact> {
  81. private Context context;
  82. private List<Contact> contactList = new ArrayList<>();
  83. public ContactAdapter(@NonNull Context context, ArrayList<Contact> list) {
  84. super(context, 0 , list);
  85. this.context = context;
  86. contactList = list;
  87. }
  88. @NonNull
  89. @Override
  90. public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
  91. View listItem = convertView;
  92. if(listItem == null)
  93. listItem = LayoutInflater.from(context).inflate(R.layout.contact_item,parent,false);
  94.  
  95. final Contact currentContact = contactList.get(position);
  96.  
  97. TextView name = (TextView) listItem.findViewById(R.id.textView);
  98. name.setText(" " + currentContact.getUser());
  99.  
  100. TextView release = (TextView) listItem.findViewById(R.id.textView2);
  101. release.setText(" " + currentContact.getLastMsg());
  102.  
  103. return listItem;
  104. }
  105. }
  106. public class Chat {
  107. private int type;
  108. private String msg;
  109. private long time;
  110. public Chat(int type,String msg,long time){this.type = type; this.msg = msg; this.time = time;}
  111. public int getType(){return type;}
  112. public void setType(int type){this.type = type;}
  113. public String getMsg(){return msg;}
  114. public void setMsg(String msg){this.msg = msg;}
  115. public long getTime(){return time;}
  116. public void setTime(long time){this.time = time;}
  117. }
  118.  
  119. {
  120. "Messages" : {
  121. "pranavk28" : {
  122. "sunilk26" : {
  123. "-LFRGvG86BpKNYBONRKX" : {
  124. "msg" : "hii",
  125. "time" : 1529482819019,
  126. "type" : 1
  127. },
  128. "-LFRH3nTMYv-QcqXkFFb" : {
  129. "msg" : "how are you?",
  130. "time" : 1529482858064,
  131. "type" : 1
  132. },
  133. "-LFRIWJvmACC61niuN07" : {
  134. "msg" : "How was your day?",
  135. "time" : 1529483237050,
  136. "type" : 1
  137. }
  138. }
  139. },
  140. "sunilk26" : {
  141. "pranavk28" : {
  142. "-LFRGvG9JJcpprSKz8YB" : {
  143. "msg" : "hii",
  144. "time" : 1529482819020,
  145. "type" : 2
  146. },
  147. "-LFRH3nTMYv-QcqXkFFc" : {
  148. "msg" : "how are you?",
  149. "time" : 1529482858064,
  150. "type" : 2
  151. },
  152. "-LFRIWJwEJ_ypA1zeBXt" : {
  153. "msg" : "How was your day?",
  154. "time" : 1529483237051,
  155. "type" : 2
  156. }
  157. }
  158. }
  159. },
  160. "Users" : {
  161. "pranavk28" : {
  162. "email" : "pranavk28@gmail.com",
  163. "name" : "Pranav Bhardwaj",
  164. "password" : "priyamkr",
  165. "username" : "pranavk28"
  166. },
  167. "rockykbc" : {
  168. "email" : "rocky6@hotmail.com",
  169. "name" : "Ravi Kumar",
  170. "password" : "neetukbc",
  171. "username" : "rockykbc"
  172. },
  173. "sunilk26" : {
  174. "email" : "thakur@gmail.com",
  175. "name" : "D K Thakur",
  176. "password" : "pass1234",
  177. "username" : "sunilk26"
  178. }
  179. }
  180. }
Add Comment
Please, Sign In to add comment