转自:http://hilary3113.iteye.com/blog/998352
listview加载adapter过程是这样的.
1 先判断adapter 有多少数据项,根据这个数据确定有多少item.
2 确定每个item里加载哪个View.
3 把View里加载要显示的数据.
main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:layout_width="wrap_content"
- android:layout_height="wrap_content" android:text="123424312423142134" />
- <ListView android:id="@+id/list" android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- </LinearLayout>
listView布局文件:item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal" android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextView android:id="@+id/name" android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView android:id="@+id/sex" android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- <TextView android:id="@+id/age" android:layout_width="wrap_content"
- android:layout_height="wrap_content" />
- </LinearLayout>
定义一个类,用来传值 People.java
- public class People {
- public String name;
- public String sex;
- public String age;
- }
定义适配器 MyAdapter.java
- package com.action;
- import java.util.ArrayList;
- import java.util.List;
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.TextView;
- /**
- *@Author: hilary
- *@Date: 2011-4-11
- **/
- public class MyAdapter extends BaseAdapter {
- private List<People> list = new ArrayList<People>();
- // private People people = new People();
- private Context context;
- public MyAdapter(Context context){
- this.context = context;
- }
- //适配器根据getCount()函数来确定要加载多少项
- @Override
- public int getCount() {
- return list.size();
- }
- @Override
- public Object getItem(int paramInt) {
- return list.get(paramInt);
- }
- @Override
- public long getItemId(int paramInt) {
- return paramInt;
- }
- /*
- * 当列表里的每一项显示到界面时,都会调用这个方法一次,并返回一个view 所以方法里面尽量要简单,不要做没必要的动作(non-Javadoc)
- * 我这里为了大家好理解,没有做优化
- */
- @Override
- public View getView(int paramInt, View paramView, ViewGroup paramViewGroup) {
- //得到列表样式的view对象
- paramView=LayoutInflater.from(context).inflate(R.layout.item, null);
- //通过view来得到Item中的每个控件的操作权
- TextView name = (TextView)paramView.findViewById(R.id.name);
- TextView sex = (TextView)paramView.findViewById(R.id.sex);
- TextView age = (TextView)paramView.findViewById(R.id.age);
- //获得list里面的第paramInt个对象,并把值赋给每个控件
- People people = list.get(paramInt);
- name.setText(people.name);
- sex.setText(people.sex);
- age.setText(people.age);
- //把一项返回,加载这个View
- return paramView;
- }
- public void addList(People people){
- list.add(people);
- }
- }
主类
- package com.action;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- public class Tab extends Activity {
- MyAdapter adapter ;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- System.out.println("*********Tab");
- ListView listView =(ListView) findViewById(R.id.list);
- adapter = new MyAdapter(this);
- setPeople();
- listView.setAdapter(adapter);
- }
- public void setPeople(){
- People people;
- for(int i=1;i<5;i++){
- people = new People();
- people.name="张三";
- people.sex = "男";
- people.age ="22";
- adapter.addList(people);
- }
- }
- }