一、首先我们先实现ListView的构建工作
1.定义一个实体类,作为ListView适配器的适配类型。新建Fruit类,代码如下:
package org.lxh.demo;
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;
}
}
2.然后需要为ListView的子项指定一个我们自定义的布局,在layout目录下新建fruit_item.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="@+id/fruit_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/fruit_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="10sp" />
</LinearLayout>
3.接下来我们要创建一个适配器,这个适配器继承自ArrayAdapter,并指定泛型类型为Fruit类。新建FruitAdapter,代码如下:
package org.lxh.demo;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import and