SHARE
TWEET

adapter

a guest Apr 21st, 2019 72 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.portal_informasi;
  2.  
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.support.v7.widget.RecyclerView;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.widget.ImageView;
  10. import android.widget.TextView;
  11.  
  12. import com.example.portal_informasi.RESPONSE.BeritaItem;
  13. import com.squareup.picasso.Picasso;
  14.  
  15. import java.util.List;
  16.  
  17. class AdapterBerita extends RecyclerView.Adapter<AdapterBerita.MyViewHolder> {
  18.     // Buat Global variable untuk manampung context
  19.     Context context;
  20.     List<BeritaItem> berita;
  21.     public AdapterBerita(Context context, List<BeritaItem> data_berita) {
  22.         // Inisialisasi
  23.         this.context = context;
  24.         this.berita = data_berita;
  25.     }
  26.     @Override
  27.     public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
  28.         // Layout inflater
  29.         View view = LayoutInflater.from(context).inflate(R.layout.berita_item, parent, false);
  30.  
  31.         // Hubungkan dengan MyViewHolder
  32.         MyViewHolder holder = new MyViewHolder(view);
  33.         return holder;
  34.     }
  35.  
  36.     @Override
  37.     public void onBindViewHolder(MyViewHolder holder, final int position) {
  38.         // Set widget
  39.         holder.tvJudul.setText(berita.get(position).getJudulBerita());
  40.         holder.tvTglTerbit.setText(berita.get(position).getTanggalPosting());
  41.  
  42.         // Dapatkan url gambar
  43.         final String urlGambarBerita = "http://192.168.100.168/rx/images/" + berita.get(position).getFoto();
  44.         // Set image ke widget dengna menggunakan Library Piccasso
  45.         // krena imagenya dari internet
  46.         Picasso.with(context).load(urlGambarBerita).into(holder.ivGambarBerita);
  47.  
  48.         // Event klik ketika item list nya di klik
  49.         holder.itemView.setOnClickListener(new View.OnClickListener() {
  50.             @Override
  51.             public void onClick(View view) {
  52.                 // Mulai activity Detail
  53.                 Intent varIntent = new Intent(context, detail.class);
  54.                 // sisipkan data ke intent
  55.                 varIntent.putExtra("JDL_BERITA", berita.get(position).getJudulBerita());
  56.                 varIntent.putExtra("TGL_BERITA", berita.get(position).getTanggalPosting());
  57.                 varIntent.putExtra("PNS_BERITA", berita.get(position).getPenulis());
  58.                 varIntent.putExtra("FTO_BERITA", urlGambarBerita);
  59.                 varIntent.putExtra("ISI_BERITA", berita.get(position).getIsiBerita());
  60.  
  61.                 // method startActivity cma bisa di pake di activity/fragment
  62.                 // jadi harus masuk ke context dulu
  63.                 context.startActivity(varIntent);
  64.             }
  65.         });
  66.     }
  67.     // Menentukan Jumlah item yang tampil
  68.     @Override
  69.     public int getItemCount() {
  70.         return berita.size();
  71.     }
  72.  
  73.     public class MyViewHolder extends RecyclerView.ViewHolder {
  74.         // Deklarasi widget
  75.         ImageView ivGambarBerita;
  76.         TextView tvJudul, tvTglTerbit, tvPenulis;
  77.         public MyViewHolder(View itemView) {
  78.             super(itemView);
  79.             // inisialisasi widget
  80.             ivGambarBerita = (ImageView) itemView.findViewById(R.id.ivPosterBerita);
  81.             tvJudul = (TextView) itemView.findViewById(R.id.tvJudulBerita);
  82.             tvTglTerbit = (TextView) itemView.findViewById(R.id.tvTglTerbit);
  83.             tvPenulis = (TextView) itemView.findViewById(R.id.tvPenulis);
  84.         }
  85.     }
  86.  
  87. }
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