JAVA   99

Untitled

Guest on 17th June 2022 08:58:11 PM

  1. public class ChatAdapter extends FirebaseRecyclerAdapter<ChatMessageModel, ChatAdapter.MyViewHolder> {
  2.     public static final int MESSAGE_SENT = 1;
  3.     public static final int MESSAGE_RECEIVED = 2;
  4.     private ArrayList<ChatMessageModel> list;
  5.  
  6.  
  7.     public ChatAdapter(FirebaseRecyclerOptions<ChatMessageModel> messages) {
  8.         super(messages);
  9.     }
  10.  
  11.  
  12.     @Override
  13.     public int getItemViewType(int position) {
  14.         switch (position) {
  15.             case 1:
  16.                 return MESSAGE_SENT;
  17.             default:
  18.                 return MESSAGE_RECEIVED;
  19.         }
  20.     }
  21.  
  22.     @NonNull
  23.     @Override
  24.     public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  25.  
  26.         int layoutRes = 0;
  27.  
  28.         //if viewType == 1(sent_message int)
  29.         switch (viewType) {
  30.             case MESSAGE_SENT:
  31.  
  32.                 //then layoutRes == 1
  33.                 layoutRes = R.layout.sent_message_item;
  34.                 break;
  35.  
  36.             case MESSAGE_RECEIVED:
  37.  
  38.                 //else, layoutRes == 2
  39.                 layoutRes = R.layout.received_message_item;
  40.                 break;
  41.         }
  42.         //inflating the view with layoutRes
  43.         View view = LayoutInflater.from(parent.getContext()).inflate(layoutRes, parent, false);
  44.         return new MyViewHolder(view);
  45.     }
  46.  
  47.  
  48.     @Override
  49.     protected void onBindViewHolder(@NonNull MyViewHolder holder, int position, ChatMessageModel model) {
  50.  
  51.         //if viewType == 1, set and display message from sent_message view
  52.         switch (holder.getItemViewType()) {
  53.             case 1:
  54.                 MyViewHolder viewHolderCurrentSent = (MyViewHolder) holder;
  55.                 viewHolderCurrentSent.mSentMessage.setText(model.getMessage());
  56.                 viewHolderCurrentSent.mSentMessage.setOnClickListener(new View.OnClickListener() {
  57.                     @Override
  58.                     public void onClick(View view) {
  59.                         deleteMessage(holder.getAbsoluteAdapterPosition());
  60.                     }
  61.                 });
  62.                 break;
  63.  
  64.             //if viewType == 2, set and display message from received_message view
  65.             case 2:
  66.                 MyViewHolder viewHolderCurrentReceived = (MyViewHolder) holder;
  67.                 viewHolderCurrentReceived.mReceivedMessage.setText(model.getMessage());
  68.                 viewHolderCurrentReceived.mReceivedMessageUser.setText(model.getMessageUser());
  69.                 break;
  70.         }
  71.  
  72.     }
  73.  
  74.  
  75.     public static class MyViewHolder extends RecyclerView.ViewHolder {
  76.         //views for received_message_item layout
  77.         TextView mReceivedMessage, mReceivedMessageUser, mReceivedMessageDate, mReceivedMessageTime;
  78.  
  79.         //views for sent_message_item layout
  80.         TextView mSentMessage, mSentMessageTime, mSentMessageDate;
  81.  
  82.         LinearLayout mSentChatContainer;
  83.  
  84.         public MyViewHolder(@NonNull View itemView) {
  85.             super(itemView);
  86.  
  87.             //views for received_message_item layout
  88.             mReceivedMessage = itemView.findViewById(R.id.message_received);
  89.             mReceivedMessageUser = itemView.findViewById(R.id.user);
  90.             mReceivedMessageDate = itemView.findViewById(R.id.date_received);
  91.             mReceivedMessageTime = itemView.findViewById(R.id.time_received);
  92.  
  93.             //views for sent_message_item layout
  94.             mSentMessage = itemView.findViewById(R.id.message);
  95.             mSentMessageTime = itemView.findViewById(R.id.time_sent);
  96.             mSentMessageDate = itemView.findViewById(R.id.date_sent);
  97.             mSentChatContainer = itemView.findViewById(R.id.chat_container);
  98.         }
  99.     }
  100.     public void deleteMessage(int position) {
  101.         DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
  102.         Query messageQuery = databaseReference.child("-N4hRQ_csYYVsSTA6V6Y").orderByChild("message").equalTo(position);
  103.  
  104.         messageQuery.addListenerForSingleValueEvent(new ValueEventListener() {
  105.             @Override
  106.             public void onDataChange(@NonNull DataSnapshot snapshot) {
  107.                 for(DataSnapshot messageSnapShot : snapshot.getChildren()) {
  108.                     messageSnapShot.getRef().removeValue();
  109.                 }
  110.             }
  111.  
  112.             @Override
  113.             public void onCancelled(@NonNull DatabaseError error) {
  114.  
  115.             }
  116.         });
  117.  
  118.     }
  119.     }

Raw Paste


Login or Register to edit or fork this paste. It's free.