首页 > 代码库 > Android控件之ListView的使用

Android控件之ListView的使用

ListView是Android当中一个非常常用的数据显示控件。

 

第一种可以使用List<HashMap<String , Object>>,作为适配器的数据源来显示要显示的数据。

 

XML布局文件之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:id="@+id/itemTitle"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:textSize="20dip" >
    </TextView>

    <TextView
        android:id="@+id/itemText"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" >
    </TextView>

</LinearLayout>

 

XML布局文件之activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <ListView android:layout_width="wrap_content"
        android:layout_height="wrap_content" 
        android:id="@+id/MyListView">
    </ListView>

</RelativeLayout>

 

JAVA源代码:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.os.Bundle;
import android.app.Activity;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

    private ListView listView = null;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) this.findViewById(R.id.MyListView);    
        //组织数据源
        List<HashMap<String , String>> myList = new ArrayList<HashMap<String,String>>();
        for(int i=0; i<10; i++){
            HashMap<String , String> map = new HashMap<String, String>();
            map.put("itemTitle", "这是一个标题");
            map.put("itemText", "这是一个内容");
            myList.add(map);
        }
        //新建i适配器把数据源加入适配器
        SimpleAdapter simpleAdapter = new SimpleAdapter(this
                , myList
                , R.layout.item
                , new String[] { "itemTitle" , "itemText" }
                , new int[] { R.id.itemTitle , R.id.itemText});
        //给list添加适配器
        listView.setAdapter(simpleAdapter);
    }

}

 

  界面显示:

 

第二种可以自定义一个Adapter类。只要写一个类继承ArrayAdapter,然后实现它的构造函数XXAdapter(Context context, int resource , List objects)和 重写它的

getCount(),getItem(int position),getItemId(int position),getView(int position, View convertView, ViewGroup parent) 等方法即可。

 

XML布局文件之activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/id"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_marginLeft="30dip"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/name"
        android:layout_width="100dip"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/id"
        android:layout_toRightOf="@id/id"
        android:textSize="20dip" />

    <TextView
        android:id="@+id/age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignTop="@id/name"
        android:layout_toRightOf="@id/name"
        android:textSize="20dip" />

</RelativeLayout>

 

JAVA源代码之Person.java

public class Person {
    
    private String id;
    private String name;
    private String age;
    
    public Person(String id, String name, String age) {
        super();
        this.id = id;
        this.name = name;
        this.age = age;
    }
    
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAge() {
        return age;
    }
    public void setAge(String age) {
        this.age = age;
    }
    
}

 

JAVA源代码之PersonAdapter.java

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class PersonAdapter extends ArrayAdapter {

    private LayoutInflater layoutInflater = null;
    private List<Person> persons;

    public PersonAdapter(Context context, int resource , List objects) {
        super(context, resource);
        layoutInflater = LayoutInflater.from(context);
        persons = objects;
    }
    
    /**
     * 获取adapter里有多少个数据项
     */
    @Override
    public int getCount() {
        return persons.size();
    }

    @Override
    public Object getItem(int position) {
        return persons.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    
    /**
     * 创建显示的数据界面
     * Adapter的作用就是ListView界面与数据之间的桥梁
     * 当列表里的每一项显示到页面时,都会调用Adapter的getView方法返回一个View
     */
    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //优化后
        ViewHolder holder;
        if(convertView == null){
            convertView = layoutInflater.inflate(R.layout.activity_main, null);
            holder = new ViewHolder();
            holder.id = (TextView)convertView.findViewById(R.id.id);
            holder.name = (TextView)convertView.findViewById(R.id.name);
            holder.age = (TextView)convertView.findViewById(R.id.age);
            convertView.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }     
        holder.id.setText(persons.get(position).getId());
        holder.name.setText(persons.get(position).getName());
        holder.age.setText(persons.get(position).getAge());
        return convertView;
    }
    
    private static class ViewHolder{
        private TextView id;
        private TextView name;
        private TextView age;
    }

}

 

JAVA源代码之MainActivity.java

import java.util.ArrayList;
import android.os.Bundle; import android.app.ListActivity; import android.view.View; import android.widget.ListView; import android.widget.Toast; public class MainActivity extends ListActivity { private ArrayList<Person> persons = new ArrayList<Person>(); private PersonAdapter personAdapter = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initData(); /** * 第一个参数:当前类的对象 * 第二个参数:布局文件 * 第三个参数:数据源(这里主要是实体对象的数据源) */ personAdapter = new PersonAdapter(MainActivity.this, R.layout.activity_main, persons); setListAdapter(personAdapter); registerForContextMenu(getListView()); } @Override protected void onListItemClick(ListView listView, View view, int position, long id) { super.onListItemClick(listView, view, position, id); Person person = persons.get(position); Toast.makeText( MainActivity.this, person.getId() + ":" + person.getName() + ":" + person.getAge(), Toast.LENGTH_LONG).show(); return; } /** * 初始化数据源 * 在PersonAdapter里会对布局文件和这里的数据源进行绑定 */ private void initData() { persons.add(new Person("序号", "姓名", "年龄")); persons.add(new Person("1", "chen1", "23")); persons.add(new Person("2", "chen2", "23")); persons.add(new Person("3", "chen3", "23")); persons.add(new Person("4", "chen4", "23")); persons.add(new Person("5", "chen5", "23")); persons.add(new Person("6", "chen6", "23")); } }

 

  界面显示: