SHARE
TWEET

GoodsAdapter

Savely_Prokhorov Oct 19th, 2019 67 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.prokhorov.styleruhw4;
  2.  
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.ImageView;
  8. import android.widget.TextView;
  9.  
  10. import androidx.annotation.NonNull;
  11. import androidx.recyclerview.widget.RecyclerView;
  12.  
  13. import com.bumptech.glide.Glide;
  14.  
  15. import java.io.FileNotFoundException;
  16. import java.util.ArrayList;
  17.  
  18. import butterknife.BindView;
  19. import butterknife.ButterKnife;
  20.  
  21. public class GoodsAdapter extends RecyclerView.Adapter<GoodsAdapter.ViewHolder> {
  22.     private final ArrayList<Product> goods = new ArrayList<>();
  23.  
  24.     @NonNull
  25.     @Override
  26.     public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
  27.         Context context = parent.getContext();
  28.         LayoutInflater layoutInflater = LayoutInflater.from(context);
  29.         View view = layoutInflater.inflate(R.layout.goods_item, parent, false);
  30.         return new ViewHolder(view);
  31.     }
  32.  
  33.     @Override
  34.     public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
  35.         Product product = goods.get(position);
  36.         String title = product.getTitle();
  37.         int price = product.getPrice();
  38.         String imageUrl = product.getImgUrl();
  39.         String emptyImgHolder = "http://askon-agro.ru/assets/images/no-photo.png";
  40.  
  41.         holder.titleTV.setText(title);
  42.         holder.priceTV.setText(String.valueOf(price));
  43.         try{
  44.             Glide
  45.                     .with(holder.imageView.getContext())
  46.                     .load(imageUrl)
  47.                     .into(holder.imageView);
  48.         } catch(Exception e){
  49.             Glide
  50.                     .with(holder.imageView.getContext())
  51.                     .load(emptyImgHolder)
  52.                     .into(holder.imageView);
  53.         }
  54.     }
  55.  
  56.     @Override
  57.     public int getItemCount() {
  58.         return goods.size();
  59.     }
  60.  
  61.     public void replaceGoods(ArrayList<Product> goods) {
  62.         this.goods.clear();
  63.         this.goods.addAll(goods);
  64.         notifyDataSetChanged();
  65.     }
  66.  
  67.     class ViewHolder extends RecyclerView.ViewHolder {
  68.         @BindView(R.id.title)
  69.         TextView titleTV;
  70.         @BindView(R.id.price)
  71.         TextView priceTV;
  72.         @BindView(R.id.image)
  73.         ImageView imageView;
  74.  
  75.         private ViewHolder(@NonNull View itemView) {
  76.             super(itemView);
  77.             ButterKnife.bind(this, itemView);
  78.         }
  79.     }
  80. }
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