skip to Main Content

WordAdapter.java

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

public class WordAdapter extends RecyclerView.Adapter<WordAdapter.wordViewHolder> {

    public ArrayList<WordClass> mWordList;

    public WordAdapter(ArrayList<WordClass> wordList) {
        mWordList = wordList;
    }

    public static class wordViewHolder extends RecyclerView.ViewHolder<> {
        public ImageView mImageview;
        public TextView mText1;
        public TextView mText2;

        public wordViewHolder(@NonNull View itemView) {
            super(itemView);
            mImageview = itemView.findViewById(R.id.image_view);
            mText1 = itemView.findViewById(R.id.text1);
            mText2 = itemView.findViewById(R.id.text2);
        }
    }

    @NonNull
    @Override
    public wordViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.list_item, viewGroup, false);
        //call custom item and connect it with holder
        wordViewHolder v = new wordViewHolder(view);
        return v;
    }

    @Override
    public void onBindViewHolder(@NonNull wordViewHolder wordViewHolder, int position) {
        //pass the values into the views
        WordClass currentWord = mWordList.get(position);
        wordViewHolder.mImageview.setImageResource(currentWord.getmImageView());
        wordViewHolder.mText1.setText(currentWord.getmTextView1());
        wordViewHolder.mText2.setText(currentWord.getmTextview2());
    }

    @Override
    public int getItemCount() {
        //count the number items in the RV
        return mWordList.size();
    }
}
انتقل للدرس الثالث ,,

اترك تعليقاً

لن يتم نشر عنوان بريدك الإلكتروني. الحقول الإلزامية مشار إليها بـ *