(FRONT) FRONT (2025)

Return Using Query and Cursor to read data sequence and fill DataAdapter for RecyclerView

   1:  package com.voipplus.mmsclient.testContacts;
   2:  
   3:  import android.util.Log;
   4:  import android.view.LayoutInflater;
   5:  import android.view.View;
   6:  import android.view.ViewGroup;
   7:  
   8:  import androidx.annotation.NonNull;
   9:  import androidx.recyclerview.widget.RecyclerView;
  10:  
  11:  import com.voipplus.mmsclient.R;
  12:  
  13:  import java.util.List;
  14:  
  15:  public class ContactsAdapter extends RecyclerView.Adapter<ContactViewHolder> {
  16:  
  17:      private List<Contact> contactsList;
  18:      private OnContactClickListener listener;
  19:      private static final String TAG = "ContactsAdapter"; // Add a TAG for logging
  20:  
  21:      // Interface for handling contact clicks
  22:      public interface OnContactClickListener {
  23:          void onContactClick(Contact contact);
  24:      }
  25:  
  26:      // Constructor to initialize the adapter with the contact list and the click listener
  27:      public ContactsAdapter(List<Contact> contactsList, OnContactClickListener listener) {
  28:          this.contactsList = contactsList;
  29:          this.listener = listener;
  30:      }
  31:  
  32:      @NonNull
  33:      @Override
  34:      public ContactViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  35:          // Inflate the layout for each contact item
  36:          View itemView = LayoutInflater.from(parent.getContext())
  37:                  .inflate(R.layout.item_contact, parent, false);
  38:          // Create and return a new ContactViewHolder with the inflated view
  39:          return new ContactViewHolder(itemView);
  40:      }
  41:  
  42:      @Override
  43:      public void onBindViewHolder(@NonNull ContactViewHolder holder, int position) {
  44:          // Get the contact at the current position
  45:          Contact contact = contactsList.get(position);
  46:          Log.d(TAG, "onBindViewHolder: Binding contact: " + contact.getName() + ", " + contact.getNumber());
  47:  
  48:          // Bind the contact data to the view holder
  49:          holder.nameTextView.setText(contact.getName());
  50:          holder.numberTextView.setText(contact.getNumber());
  51:  
  52:          // Set click listener for the item view
  53:          holder.itemView.setOnClickListener(v -> {
  54:              // Notify the listener that a contact has been clicked
  55:              if (listener != null) {
  56:                  Log.d(TAG, "onBindViewHolder: Contact clicked: " + contact.getName() + ", " + contact.getNumber());
  57:                  listener.onContactClick(contact);
  58:              }
  59:          });
  60:      }
  61:  
  62:      @Override
  63:      public int getItemCount() {
  64:          // Return the number of contacts in the list
  65:          return contactsList.size();
  66:      }
  67:  }
  68:  

Return




AndroidMosaic context:



Comments ( )
Link to this page: http://www.vb-net.com/AndroidMosaic/ContactAdapter.htm
< THANKS ME>