如何覆盖getItem()?

时间:2015-06-05 02:51:27

标签: android android-arrayadapter android-adapter

这些是代码的一部分:

public class Fruit {
    private String name;
    private int imageId;
    public Fruit(String name, int imageId) {

        this.name = name;
        this.imageId = imageId;
    }
    public String getName() {
       return name;
 }
    public int getImageId() {
       return imageId;
    }
}
public class FruitAdapter extends ArrayAdapter<Fruit> {

       private int resourceId;

       public FruitAdapter(Context context, int textViewResourceId,
           List<Fruit> objects) {
               super(context, textViewResourceId, objects);
               resourceId = textViewResourceId;
       }
       @Override
       public View getView(int position, View convertView, ViewGroup parent){
          Fruit fruit = getItem(position); // get the current Fruit instance.
          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;
  }
}
public class MainActivity extends Activity {

public List<Fruit> fruitList = new ArrayList<Fruit>();

@Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    initFruits();

    FruitAdapter adapter = new FruitAdapter(MainActivity.this,
            R.layout.fruit_item, fruitList);
    ListView listView = (ListView)findViewById(R.id.list_view);
    listView.setAdapter(adapter);


  }

  private void initFruits() {
    Fruit apple = new Fruit("Apple", R.drawable.apple);
    fruitList.add(apple);
    Fruit banana = new Fruit("Banana", R.drawable.banana);
    fruitList.add(banana);
    Fruit orange = new Fruit("Orange", R.drawable.orange);
    fruitList.add(orange);
    Fruit watermelon = new Fruit("Watermelon", R.drawable.watermelon);
    fruitList.add(watermelon);
    Fruit pear = new Fruit("Pear", R.drawable.pear);
    fruitList.add(pear);
    Fruit grape = new Fruit("Grape", R.drawable.grape);
    fruitList.add(grape);
    Fruit pineapple = new Fruit("Pineapple", R.drawable.pineapple);
    fruitList.add(pineapple);
    Fruit strawberry = new Fruit("Strawberry", R.drawable.strawberry);
    fruitList.add(strawberry);
    Fruit cherry = new Fruit("Cherry", R.drawable.cherry);
    fruitList.add(cherry);
    Fruit mango = new Fruit("Mango", R.drawable.mango);
    fruitList.add(mango);
  }
}

getItem()方法返回String,但我想要一个Fruit实例。如何覆盖它?我不知道如何使用位置参数。

以上都是代码。我没有放XML.marater_item.xml包含TextView和ImageView。

1 个答案:

答案 0 :(得分:0)

将列表分配给类变量并访问它

public class FruitAdapter extends ArrayAdapter<Fruit> {

       private int resourceId;
       private List<Fruit> mObjects;     
       public FruitAdapter(Context context, int textViewResourceId,
           List<Fruit> objects) {
               super(context, textViewResourceId, objects);
               resourceId = textViewResourceId;
                mObjects =objects;
       }
       @Override
       public View getView(int position, View convertView, ViewGroup parent){
          Fruit fruit = mObjexts.get(position); // get the current Fruit instance.
          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;
  }
}

你也可以覆盖getItem()

@Override
       public Fruit getItem(int position) {
             mObjects.get[position];
       }