直接上代码吧---
Fruitactity.java(MainActity)
package com.lhp.basic;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import com.lhp.basic.adapter.FruitAdapter;
import com.lhp.basic.domain.Fruit;
import java.util.ArrayList;
import java.util.List;
public class FruitActity extends AppCompatActivity {
List<Fruit> fruitList=new ArrayList<Fruit>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fruit_actity);
initFruits(); // 初始化水果数据
FruitAdapter adapter = new FruitAdapter(FruitActity.this,
R.layout.fruit_item, fruitList);
ListView listView = (ListView) findViewById(R.id.list_view);
listView.setAdapter(adapter);
}
public void initFruits(){
for (int i = 0; i < 2; i++) {
Fruit apple = new Fruit("Apple", R.drawable.chat);
fruitList.add(apple);
Fruit banana = new Fruit("Banana", R.drawable.heart);
fruitList.add(banana);
Fruit orange = new Fruit("Orange", R.drawable.like);
fruitList.add(orange);
Fruit watermelon = new Fruit("Watermelon", R.drawable.item);
fruitList.add(watermelon);
Fruit pear = new Fruit("Pear", R.drawable.like);
fruitList.add(pear);
Fruit grape = new Fruit("Grape", R.drawable.chat);
fruitList.add(grape);
Fruit pineapple = new Fruit("Pineapple", R.drawable.heart);
fruitList.add(pineapple);
Fruit strawberry = new Fruit("Strawberry", R.drawable.share);
fruitList.add(strawberry);
Fruit cherry = new Fruit("Cherry", R.drawable.location);
fruitList.add(cherry);
Fruit mango = new Fruit("Mango", R.drawable.plant);
fruitList.add(mango);
}
}
}
FruitAdapter.java
package com.lhp.basic.adapter;
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 com.lhp.basic.R;
import com.lhp.basic.domain.Fruit;
import java.util.List;
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);//获取当前实例
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;
}
}
fruit_actity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
fruit_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<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:textColor="@color/colorPrimary"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
效果图