Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ChatActivity extends AppCompatActivity {
- DatabaseReference ref,ref1;
- TextView t;
- EditText e;
- ImageButton b;
- String contact,user;
- ArrayList<Chat> chatList;
- ListView listView;
- ChatAdapter chatAdapter;
- String msg;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_chat);
- chatList = new ArrayList<>();
- Intent i = getIntent();
- contact = i.getStringExtra("contact");
- user = i.getStringExtra("user");
- t = findViewById(R.id.textView3);
- e = findViewById(R.id.editText8);
- b = findViewById(R.id.imageButton);
- listView = findViewById(R.id.listView2);
- t.setText(" " + contact);
- ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
- ref.addChildEventListener(new ChildEventListener() {
- @Override
- public void onChildAdded(DataSnapshot dataSnapshot, String s) {
- if (!dataSnapshot.exists()) {
- return;
- }
- Toast.makeText(getApplicationContext(), "onChildAdded of ref", Toast.LENGTH_LONG).show();
- while(!(dataSnapshot.hasChild("type") && dataSnapshot.hasChild("msg") && dataSnapshot.hasChild("time"))){}
- chatList.add(new Chat(dataSnapshot.child("type").getValue(Integer.class), dataSnapshot.child("msg").getValue(String.class), dataSnapshot.child("time").getValue(Long.class)));
- if(chatAdapter!=null)
- chatAdapter.notifyDataSetChanged();
- }
- @Override
- public void onChildChanged(DataSnapshot dataSnapshot, String s) {
- }
- @Override
- public void onChildRemoved(DataSnapshot dataSnapshot) {
- }
- @Override
- public void onChildMoved(DataSnapshot dataSnapshot, String s) {
- }
- @Override
- public void onCancelled(DatabaseError databaseError) {
- }
- });
- b.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- msg = e.getText().toString();
- ref = FirebaseDatabase.getInstance().getReference().child("Messages").child(user).child(contact);
- ref1 = FirebaseDatabase.getInstance().getReference().child("Messages").child(contact).child(user);
- ref = ref.push();
- ref1 = ref1.push();
- ref.child("type").setValue(1);
- ref1.child("type").setValue(2);
- ref.child("msg").setValue(msg);
- ref1.child("msg").setValue(msg);
- ref.child("time").setValue(ServerValue.TIMESTAMP);
- ref1.child("time").setValue(ServerValue.TIMESTAMP);
- }
- });
- chatAdapter = new ChatAdapter(this, chatList);
- listView.setAdapter(chatAdapter);
- }
- }
- public class ContactAdapter extends ArrayAdapter<Contact> {
- private Context context;
- private List<Contact> contactList = new ArrayList<>();
- public ContactAdapter(@NonNull Context context, ArrayList<Contact> list) {
- super(context, 0 , list);
- this.context = context;
- contactList = list;
- }
- @NonNull
- @Override
- public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
- View listItem = convertView;
- if(listItem == null)
- listItem = LayoutInflater.from(context).inflate(R.layout.contact_item,parent,false);
- final Contact currentContact = contactList.get(position);
- TextView name = (TextView) listItem.findViewById(R.id.textView);
- name.setText(" " + currentContact.getUser());
- TextView release = (TextView) listItem.findViewById(R.id.textView2);
- release.setText(" " + currentContact.getLastMsg());
- return listItem;
- }
- }
- public class Chat {
- private int type;
- private String msg;
- private long time;
- public Chat(int type,String msg,long time){this.type = type; this.msg = msg; this.time = time;}
- public int getType(){return type;}
- public void setType(int type){this.type = type;}
- public String getMsg(){return msg;}
- public void setMsg(String msg){this.msg = msg;}
- public long getTime(){return time;}
- public void setTime(long time){this.time = time;}
- }
- {
- "Messages" : {
- "pranavk28" : {
- "sunilk26" : {
- "-LFRGvG86BpKNYBONRKX" : {
- "msg" : "hii",
- "time" : 1529482819019,
- "type" : 1
- },
- "-LFRH3nTMYv-QcqXkFFb" : {
- "msg" : "how are you?",
- "time" : 1529482858064,
- "type" : 1
- },
- "-LFRIWJvmACC61niuN07" : {
- "msg" : "How was your day?",
- "time" : 1529483237050,
- "type" : 1
- }
- }
- },
- "sunilk26" : {
- "pranavk28" : {
- "-LFRGvG9JJcpprSKz8YB" : {
- "msg" : "hii",
- "time" : 1529482819020,
- "type" : 2
- },
- "-LFRH3nTMYv-QcqXkFFc" : {
- "msg" : "how are you?",
- "time" : 1529482858064,
- "type" : 2
- },
- "-LFRIWJwEJ_ypA1zeBXt" : {
- "msg" : "How was your day?",
- "time" : 1529483237051,
- "type" : 2
- }
- }
- }
- },
- "Users" : {
- "pranavk28" : {
- "email" : "pranavk28@gmail.com",
- "name" : "Pranav Bhardwaj",
- "password" : "priyamkr",
- "username" : "pranavk28"
- },
- "rockykbc" : {
- "email" : "rocky6@hotmail.com",
- "name" : "Ravi Kumar",
- "password" : "neetukbc",
- "username" : "rockykbc"
- },
- "sunilk26" : {
- "email" : "thakur@gmail.com",
- "name" : "D K Thakur",
- "password" : "pass1234",
- "username" : "sunilk26"
- }
- }
- }
Add Comment
Please, Sign In to add comment