Android Open Source - firstcodeandroid Fruit Adapter

From Project

Back to project page firstcodeandroid.


The source code is released under:

MIT License

If you think the Android project firstcodeandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.example.listviewtest;
//from   www . j av a2 s .co  m
import java.util.List;

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;

public class FruitAdapter extends ArrayAdapter<Fruit>{

  private int resourceId;
  public FruitAdapter(Context context, int textViewResourceId,
      List<Fruit> objects) {
    super(context, textViewResourceId, objects);
    // TODO Auto-generated constructor stub
    resourceId = textViewResourceId;
   * convertView is cached View
  public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub
    Fruit fruit = this.getItem(position);
    View view ;
    // a inner class, to not get Widget every time 
    ViewHolder viewHolder;
    // it makes : even you scroll the screen very fast, it can perform good.  
      view = LayoutInflater.from(this.getContext()).inflate(resourceId, null);
      viewHolder = new ViewHolder();
      //TODO ? save ViewHolder in View; what the detail usage 
    } else {
      view = convertView;
      viewHolder=(ViewHolder)view.getTag();// retrieve the saved ViewHolder
//    ImageView fruitImage=(ImageView)view.findViewById(;
//    TextView fruitName=(TextView)view.findViewById(;
//    fruitImage.setImageResource(fruit.getImageId());
//    fruitName.setText(fruit.getName());
    return view;
  class ViewHolder {
    ImageView fruitImage;
    TextView fruitName;


Java Source Code List