SHARE
TWEET

Untitled

a guest Jul 16th, 2019 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. public class PersonalChatAdapter extends RecyclerView.Adapter<PersonalChatAdapter.ViewHolder> {
  3.  
  4.     private Context context;
  5.     private List<Users> usersList;
  6.     private boolean isChat;
  7.  
  8.     String lastMessage;
  9.  
  10.     public PersonalChatAdapter(Context context, List<Users> usersList, boolean isChat) {
  11.         this.context = context;
  12.         this.usersList = usersList;
  13.         this.isChat = isChat;
  14.     }
  15.  
  16.  
  17.     @NonNull
  18.     @Override
  19.     public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  20.         View view = LayoutInflater.from(context).inflate(R.layout.row_chat_personal, parent, false);
  21.  
  22.         return new PersonalChatAdapter.ViewHolder(view);
  23.     }
  24.  
  25.     @Override
  26.     public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  27.         final Users user = usersList.get(position);
  28.         holder.username.setText(user.getUsername());
  29.  
  30.         if (user.getImageURL().equals("default")){
  31.             holder.profilImage.setImageResource(R.mipmap.ic_launcher);
  32.         } else {
  33.             Glide.with(context).load(user.getImageURL()).into(holder.profilImage);
  34.         }
  35.  
  36.         if (isChat){
  37.             lastMessage(user.getId(), holder.last_msg);
  38.         } else {
  39.             holder.last_msg.setVisibility(View.GONE);
  40.         }
  41.  
  42.         if (isChat){
  43.             if (user.getStatus().equals("online")){
  44.                 holder.img_on.setVisibility(View.VISIBLE);
  45.                 holder.img_off.setVisibility(View.GONE);
  46.             } else {
  47.                 holder.img_on.setVisibility(View.GONE);
  48.                 holder.img_off.setVisibility(View.VISIBLE);
  49.             }
  50.         } else{
  51.             holder.img_on.setVisibility(View.GONE);
  52.             holder.img_off.setVisibility(View.GONE);
  53.         }
  54.  
  55.         holder.itemView.setOnClickListener(new View.OnClickListener() {
  56.             @Override
  57.             public void onClick(View v) {
  58.                 Intent intent= new Intent(context, ChatActivity.class);
  59.                 intent.putExtra("userid", user.getId());
  60.                 context.startActivity(intent);
  61.             }
  62.         });
  63.     }
  64.  
  65.     @Override
  66.     public int getItemCount() {
  67.         return 0;
  68.     }
  69.  
  70.     public class ViewHolder extends RecyclerView.ViewHolder {
  71.         public TextView username, last_msg;
  72.         public ImageView profilImage;
  73.         private ImageView img_on, img_off;
  74.  
  75.  
  76.         public ViewHolder(View itemView) {
  77.             super(itemView);
  78.  
  79.             username = itemView.findViewById(R.id.tx_name);
  80.             profilImage = itemView.findViewById(R.id.img_cp_list);
  81.             img_on = itemView.findViewById(R.id.img_on);
  82.             img_off = itemView.findViewById(R.id.img_off);
  83.             last_msg = itemView.findViewById(R.id.last_msg);
  84.         }
  85.     }
  86.  
  87.     //check for last message
  88.     private void lastMessage(final String userid, final TextView last_msg){
  89.         lastMessage = "default";
  90.         final FirebaseUser firebaseUser = FirebaseAuth.getInstance().getCurrentUser();
  91.         DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Chats");
  92.  
  93.         reference.addValueEventListener(new ValueEventListener() {
  94.             @Override
  95.             public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
  96.                 for (DataSnapshot snapshot : dataSnapshot.getChildren()){
  97.                     ChatModel chat = snapshot.getValue(ChatModel.class);
  98.  
  99.                     if (chat.getReceiver().equals(firebaseUser.getUid()) && chat.getSender().equals(userid) ||
  100.                     chat.getReceiver().equals(userid) && chat.getSender().equals(firebaseUser.getUid())){
  101.                         lastMessage = chat.getMessage();
  102.                     }
  103.                 }
  104.  
  105.                 switch (lastMessage){
  106.                     case "default":
  107.                         last_msg.setText("Tidak ada Pesan");
  108.                         break;
  109.  
  110.                     default:
  111.                         last_msg.setText(lastMessage);
  112.                         break;
  113.                 }
  114.  
  115.                 lastMessage = "default"
  116. ;            }
  117.  
  118.             @Override
  119.             public void onCancelled(@NonNull DatabaseError databaseError) {
  120.  
  121.             }
  122.         });
  123.     }
  124. }
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
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top