1.新建实体类,作为ListView适配器的适配类型
public class Fruit {
private String name ;
private String price;
public Fruit(String name , String price){
this.name = name;
this.price = price;
}
public String getName(){
return name;
}
public String getPrice(){
return price;
}
}
2.新建ListView的子项的布局,名为fruit_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:id="@+id/fruit_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:id="@+id/fruit_price"/>
</LinearLayout>
3.创建一个自定义的适配器继承自ArrayAdapter,并制定泛指型为Fruit类。
public class FruitAdapter extends ArrayAdapter<Fruit> { //泛指型指定为Fruit类
private int resourceId;
public FruitAdapter(Context context,int textViewResourceId,List<Fruit> objects){
super(context,textViewResourceId,objects);
resourceId = textViewResourceId;//获取子项的id,getView()方法中要使用
}
// 当屏幕每个子项滚动到屏幕内的时候会被调用
@Override
public View getView(int position,View convertView,ViewGroup parent){
Fruit fruit = getItem(position);//获取当前项的Fruit实例
View view = LayoutInflater.from(getContext()).inflate(resourceId, null);//加载传入的布局
TextView fruitName = (TextView) view.findViewById(R.id.fruit_name); //获取布局中的id
TextView fruitPrice = (TextView) view.findViewById(R.id.fruit_price);
fruitName.setText(fruit.getName()); //加载数据
fruitPrice.setText(fruit.getPrice());
return view;
}
}
4.添加activity_main布局中的ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/list_view">
</ListView>
</LinearLayout>
5.修改MainActivity中的代码
public class MainActivity extends Activity {
private List<Fruit> fruitList = new ArrayList<Fruit>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initFruits(); //初始化数据
// 在FruitAdapter继承时已经指定的泛指型,所以这里不需要再指定
FruitAdapter adapter = new FruitAdapter(this,R.layout.fruit_item,fruitList);//context,layout,List<Fruit>
ListView listView = (ListView) findViewById(R.id.list_view);
// 绑定适配器
listView.setAdapter(adapter);
}
// 初始化数据
private void initFruits(){
Fruit a1 = new Fruit("1","20");
fruitList.add(a1);//将创建好的数据对象添加到水果列表中
Fruit a2 = new Fruit("1","20");
fruitList.add(a2);
Fruit a3 = new Fruit("1","20");
fruitList.add(a3);
Fruit a4 = new Fruit("1","20");
fruitList.add(a4);
Fruit a5 = new Fruit("1","20");
fruitList.add(a5);
Fruit a6= new Fruit("1","20");
fruitList.add(a6);
Fruit a7 = new Fruit("1","20");
fruitList.add(a7);
Fruit a8= new Fruit("1","20");
fruitList.add(a8);
Fruit a9 = new Fruit("1","20");
fruitList.add(a9);
Fruit a10 = new Fruit("1","20");
fruitList.add(a10);
Fruit a11 = new Fruit("1","20");
fruitList.add(a11);
Fruit a12 = new Fruit("1","20");
fruitList.add(a12);
Fruit a13 = new Fruit("1","20");
fruitList.add(a13);
Fruit a14 = new Fruit("1","20");
fruitList.add(a14);
}
}
总结:
使用ListView控件需要以下几个步骤:
①在布局中添加ListView控件
②设置适配器
③将适配器与ListView控件建立关联