import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import java.util.List;
public class FruitAdapter extends ArrayAdapter {
private int resourceId = 0;
public FruitAdapter(@NonNull Context context, int resource, @NonNull List<
Fruit
> objects) {
super(context, resource, objects);
resourceId = resource;
}
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
Fruit fruit= (Fruit) getItem(position);
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
ImageView fruitImage = (ImageView)view.findViewById(R.id.fruit_image);
TextView fruitName = (TextView)view.findViewById(R.id.fruit_name);
fruitImage.setImageResource(fruit.getImageId());
fruitName.setText(fruit.getName());
return view;
}
}