SHARE
TWEET

Untitled

a guest Jul 16th, 2019 57 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ivonkhalif.ragnarok.forumalumni.chat;
  2.  
  3.  
  4. import android.os.Bundle;
  5. import android.support.annotation.NonNull;
  6. import android.support.v4.app.Fragment;
  7. import android.support.v7.widget.LinearLayoutManager;
  8. import android.support.v7.widget.RecyclerView;
  9. import android.util.Log;
  10. import android.view.LayoutInflater;
  11. import android.view.View;
  12. import android.view.ViewGroup;
  13.  
  14. import com.google.firebase.auth.FirebaseAuth;
  15. import com.google.firebase.auth.FirebaseUser;
  16. import com.google.firebase.database.DataSnapshot;
  17. import com.google.firebase.database.DatabaseError;
  18. import com.google.firebase.database.DatabaseReference;
  19. import com.google.firebase.database.FirebaseDatabase;
  20. import com.google.firebase.database.ValueEventListener;
  21. import com.ivonkhalif.ragnarok.forumalumni.R;
  22. import com.ivonkhalif.ragnarok.forumalumni.anggota.AnggotaAdapter;
  23. import com.ivonkhalif.ragnarok.forumalumni.anggota.Users;
  24.  
  25. import java.util.ArrayList;
  26. import java.util.List;
  27.  
  28.  
  29. public class ChatFragment extends Fragment {
  30.  
  31.     private RecyclerView recyclerView;
  32.     private List<Users> users;
  33.     private PersonalChatAdapter adapter;
  34.  
  35.     FirebaseUser firebaseUser;
  36.     DatabaseReference reference;
  37.  
  38.     private List<ChatList> chatsList;
  39.  
  40.     @Override
  41.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  42.                              Bundle savedInstanceState) {
  43.  
  44.         View view = inflater.inflate(R.layout.fragment_chat, container, false);
  45.  
  46.         recyclerView = view.findViewById(R.id.recycler_chat);
  47.         recyclerView.setHasFixedSize(true);
  48.         recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
  49.  
  50.         firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  51.  
  52.         chatsList = new ArrayList<>();
  53.  
  54.         reference = FirebaseDatabase.getInstance().getReference("Chatlist").child(firebaseUser.getUid());
  55.         reference.addValueEventListener(new ValueEventListener() {
  56.             @Override
  57.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  58.                 chatsList.clear();
  59.                 for (DataSnapshot snapshot : dataSnapshot.getChildren()){
  60.                     ChatList chatList = snapshot.getValue(ChatList.class);
  61.                     chatsList.add(chatList);
  62.                 }
  63.  
  64.                 chatList();
  65.             }
  66.  
  67.             @Override
  68.             public void onCancelled(@NonNull DatabaseError databaseError) {
  69.  
  70.             }
  71.         });
  72.         return view;
  73.     }
  74.  
  75.     private void chatList() {
  76.         users = new ArrayList<>();
  77.         reference = FirebaseDatabase.getInstance().getReference("Users");
  78.  
  79.         reference.addValueEventListener(new ValueEventListener() {
  80.             @Override
  81.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  82.                 users.clear();
  83.                 for (DataSnapshot snapshot : dataSnapshot.getChildren()){
  84.                     Users user = snapshot.getValue(Users.class);
  85.                     for (ChatList chatList : chatsList){
  86.                         if (user.getId().equals(chatList.getId())){
  87.                             users.add(user);
  88.                         }
  89.                     }
  90.                 }
  91.                 adapter = new PersonalChatAdapter(getContext(), users, true);
  92.                 recyclerView.setAdapter(adapter);
  93.             }
  94.  
  95.             @Override
  96.             public void onCancelled(@NonNull DatabaseError databaseError) {
  97.  
  98.             }
  99.         });
  100.     }
  101.  
  102. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top