SHARE
TWEET

DataAdapter.java

dreamforce Oct 10th, 2019 70 in 20 days
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. public class DataAdapter extends RecyclerView.Adapter<DataAdapter.ViewHolder> {
  2.     private final String LOG_TAG = "myLogs";
  3.     private LayoutInflater inflater;
  4.     private Context context;
  5.     private List<Student> students;
  6.  
  7.     DataAdapter(Context context, List<Student> students) {
  8.         this.students = students;
  9.         this.context = context;
  10.         this.inflater = LayoutInflater.from(context);
  11.     }
  12.  
  13.     public class ViewHolder extends RecyclerView.ViewHolder {
  14.         final ImageView imageView;
  15.         final TextView nameView, infoView;
  16.  
  17.         ViewHolder(View view) {
  18.             super(view);
  19.             imageView = view.findViewById(R.id.image);
  20.             nameView = view.findViewById(R.id.name);
  21.             infoView = view.findViewById(R.id.info);
  22.  
  23.             view.setOnClickListener(new View.OnClickListener() {
  24.                 @Override
  25.                 public void onClick(View v) {
  26.                     int position = getAdapterPosition();
  27.                     Log.d(LOG_TAG, "onClick: position = " + position);
  28.                     Student selectedItem = students.get(position);
  29.                     Toast.makeText(context, selectedItem.getName(), Toast.LENGTH_SHORT).show();
  30.                 }
  31.             });
  32.         }
  33.     }
  34.  
  35.     @NonNull
  36.     @Override
  37.     public DataAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  38.         View view = inflater.inflate(R.layout.list_item, parent, false);
  39.         return new ViewHolder(view);
  40.     }
  41.  
  42.     @Override
  43.     public void onBindViewHolder(@NonNull DataAdapter.ViewHolder holder, int position) {
  44.         Student student = students.get(position);
  45.         holder.imageView.setImageResource(student.getImage());
  46.         holder.nameView.setText(student.getName());
  47.         holder.infoView.setText(student.getInfo());
  48.     }
  49.  
  50.     @Override
  51.     public int getItemCount() {
  52.         return students.size();
  53.     }
  54. }
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