MeteoriteDB

Log | Files | Refs | README

RecyclerViewAdapter.java (2429B)


      1 package com.example.project_assignment;
      2 
      3 import android.content.Context;
      4 import android.content.Intent;
      5 import android.os.Parcel;
      6 import android.os.Parcelable;
      7 import android.view.LayoutInflater;
      8 import android.view.View;
      9 import android.view.ViewGroup;
     10 import android.widget.TextView;
     11 
     12 import androidx.annotation.NonNull;
     13 import androidx.recyclerview.widget.RecyclerView;
     14 
     15 import java.util.List;
     16 
     17 public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
     18 
     19     private final List<Meteorite> meteorites;
     20     private final Context context;
     21 
     22     public RecyclerViewAdapter(Context context, List<Meteorite> meteorites) {
     23         this.context = context;
     24         this.meteorites = meteorites;
     25     }
     26 
     27     @NonNull
     28     @Override
     29     public RecyclerViewAdapter.MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
     30         LayoutInflater inflater = LayoutInflater.from(context);
     31         View view = inflater.inflate(R.layout.recycler_item, parent, false);
     32 
     33         return new RecyclerViewAdapter.MyViewHolder(view);
     34     }
     35 
     36     @Override
     37     public void onBindViewHolder(@NonNull RecyclerViewAdapter.MyViewHolder holder, int position) {
     38         holder.meteoriteName.setText(meteorites.get(position).getName());
     39         holder.meteoriteMass.setText(meteorites.get(position).getMass() + "kg");
     40         holder.MeteoriteAge.setText(meteorites.get(position).getDate());
     41     }
     42 
     43     @Override
     44     public int getItemCount() {
     45         return meteorites.size();
     46     }
     47 
     48     public class MyViewHolder extends RecyclerView.ViewHolder {
     49         TextView meteoriteName, meteoriteMass, MeteoriteAge;
     50 
     51         public MyViewHolder(@NonNull View itemView) {
     52             super(itemView);
     53 
     54             meteoriteName = itemView.findViewById(R.id.textview_meteoriteName);
     55             meteoriteMass = itemView.findViewById(R.id.textview_meteoriteMass);
     56             MeteoriteAge = itemView.findViewById(R.id.textview_meteoriteAge);
     57 
     58             itemView.setOnClickListener(new View.OnClickListener() {
     59                 @Override
     60                 public void onClick(View view) {
     61                     Intent intent = new Intent(context, MeteoriteActivity.class);
     62 
     63                     Meteorite meteorite = meteorites.get(getAdapterPosition());
     64                     intent.putExtra("meteorite", meteorite);
     65 
     66                     context.startActivity(intent);
     67                 }
     68             });
     69         }
     70     }
     71 }