package activitytest.com.example.listview; 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 java.util.List; public class FruitAdapter extends ArrayAdapter<Fruit> { private int resourceId; //依次输入 1.当前上下文 2.布局id 3.适配数据 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 fruit = getItem(position); //使用LayoutInflater来为这个子项加载我们传入的布局 View view = LayoutInflater.from(getContext()).inflate(resourceId, parent, false); //分别调用实例 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; } }
就是这张图。
这个程序就是来定制一个ListView界面的。
MR帽子先生