代码改变世界

[Android学习笔记]使用ListView

2014-03-22 02:04  hellenism  阅读(231)  评论(0编辑  收藏  举报

简单使用ListView


 

关键在于Adatper

Adatper用来连接UI与数据源。Adapter既负责提供数据,又负责创建Item视图。


 

一般步骤:

1.创建list_item.xml,用来创建ListView的Item的UI

2.自定义Adapter和数据源对象

3.在页面布局中定义ListView,在Activity中获取ListView引用

4.为ListView添加Adatper对象


 

 

Ex:

1.创建list_item.xml

<?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" >
    
           <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/name"
            android:paddingLeft="50px"
            />
        
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/age"
            android:text="a"
            android:paddingLeft="50px"
            />
        

        <TextView
            android:id="@+id/id"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:paddingLeft="50px" />

</LinearLayout>
list_item.xml

 

2.自定义Adatper

public class StudentAdapter extends BaseAdapter
{
    /**
     * 数据源 
     */
    private List<Student> students;
    
    /**
     * inflater引用 ,用来加载item.xml,获得view引用
     */
    private LayoutInflater inflater;
    
    /**
     * item.xml资源 
     */
    private int source;

    public StudentAdapter(Context context,List<Student> students , int source){
        this.students = students;
        this.source = source;
        
        //inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater = LayoutInflater.from(context);
    }
    
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return students.size();
    }
    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return students.get(arg0);
    }
    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }
    
    
    /* (non-Javadoc)
     * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
     * 创建Item视图,关联数据源
     */
    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub
        
        TextView idView = null;
        TextView nameView = null;
        TextView ageView = null;
        
        if(arg1 == null)
        {
            arg1 = this.inflater.inflate(source, null);
            
            idView = (TextView)arg1.findViewById(R.id.id);
            nameView =(TextView)arg1.findViewById(R.id.name);
            ageView = (TextView)arg1.findViewById(R.id.age);
        }
        else
        {
            idView = (TextView)arg1.findViewById(R.id.id);
            nameView =(TextView)arg1.findViewById(R.id.name);
            ageView = (TextView)arg1.findViewById(R.id.age);
        }
        
        Student stu = students.get(arg0);
        
        idView.setText(stu.getId().toString());
        nameView.setText(stu.getName());
        ageView.setText(stu.getAge().toString());
        
        return arg1;
    }

}
Adatper

 

3.在Activity中为ListView添加Adatper

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        listView = (ListView)findViewById(R.id.myListView);
        ArrayList<Student> students = new ArrayList<Student>();
        students.add(new Student("st",1,1));
        StudentAdapter aa = new StudentAdapter(this,students,R.layout.list_item);
        listView.setAdapter(aa);
    }
Activity