Home RecycleView.Adapter?
Reply: 0

RecycleView.Adapter?

Dheeraj Joshi
1#
Dheeraj Joshi Published in 2017-09-14 05:28:25Z

I have created a RecycleView Adapter but have many confuson related to this adapter , I know how to use this but not sure about the logic behing this adpater,please help

 class RecycleAdpapter extends RecyclerView.Adapter<RecycleAdpapter.ViewHolder> {
private Context cn;
private String[] desc;
private int[] img;
public RecycleAdpapter(String[] de,int[] im,Context context){
    this.desc=de;
    this.img=im;
    this.cn=context;
}

public static class ViewHolder extends RecyclerView.ViewHolder {
    private CardView cardView;

    public ViewHolder(CardView cv) {
        super(cv);
        this.cardView = cv;
    }
    }

    @Override
    public RecycleAdpapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
       Log.v("asda","THIS IS ONCREATE");
        CardView cv = (CardView) 
  LayoutInflater.from(parent.getContext()).inflate(R.layout.card, parent, false);
        return new ViewHolder(cv);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Log.v("DASD","THIS IS ONBIND");
        CardView cardView =holder.cardView;
        ImageView iv= (ImageView)cardView.findViewById(R.id.image);
        Drawable drawable =  ContextCompat.getDrawable(cn,img[position]);
        iv.setImageDrawable(drawable);
        TextView tv =(TextView)cardView.findViewById(R.id.text);
        tv.setText(desc[position]);

    }

    @Override
    public int getItemCount() {
        Log.v("hello","THIS GET CALLED");
        return desc.length;
    }


}

Q1. We are creating View holders in the onCreateViewBinder() , and binding the data in onBindViewHolder() but we are not telling the adapter that how many view holder it have to create , like i have five pizza so there is no code here asking adapter to create 5 pizza, so how it come to know?

Q2. what if i had only 2 items in the data,how will it get information about that,now i have only 2 cardview to create ,how will it know that onBindViewHolder() has to run for only two positions ?

Q3. What is the work of getItemCount()?

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.381854 second(s) , Gzip On .

© 2016 Powered by cudou.com design MATCHINFO