Android常用UI组件 - ListView

列表视图(ListView)是Android当中一个非常重要的组件,它以列表的形式展示具体内容,并且能够根据数据的长度自适应显示。

 

列表的显示需要三个元素:

1.ListVeiw 用来展示列表的View。

2.适配器 用来把数据映射到ListView上的中介。

3.数据 具体的将被映射的字符串,图片,或者基本组件。

 

首先介绍“适配器”这个基础概念。在列表中定义的数据都通过“适配器”来映射到ListView上,ListView中常用的适配器有两种:

ArrayAdapter:最简单的适配器,只能显示一行文字;

SimpleAdapter:具有很好扩展性的适配器,可以显示自定义内容。

 

用ArrayAdapter结合ListView进行显示

运行效果

 

源代码ActivityList1.java

package com.rainsong.listviewdemo;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener;  
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class ActivityList1 extends Activity
{
    ListView listView;
    List<String> data;
    ArrayAdapter arrayAdapter;
    OnItemClickListener listener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        listView = new ListView(this);
        data = new ArrayList<String>();
        data.add("Item1");
        data.add("Item2");
        data.add("Item3");
        arrayAdapter = new ArrayAdapter(this,
                android.R.layout.simple_list_item_1, data);
        listView.setAdapter(arrayAdapter);

        listener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent,
                    View view, int position, long id) {
                Toast.makeText(ActivityList1.this, parent.getItemAtPosition(position).toString() + " clicked",Toast.LENGTH_SHORT).show();
            }
        };
        listView.setOnItemClickListener(listener);

        setContentView(listView);
    }
}


API知识点

 

ArrayAdapter

public class

ArrayAdapter

extends BaseAdapter

implements Filterable


ArrayAdapter(Context context, int resource, List<T> objects)

Constructor


ListView

public class

ListView

extends AbsListView

 

ListView(Context context)


void

setAdapter(ListAdapter adapter)

Sets thedata behind this ListView.

 

void

setOnItemClickListener(AdapterView.OnItemClickListener listener)

Register acallback to be invoked when an item in this AdapterView has been clicked.


AdapterView.OnItemClickListener

 public static interface

AdapterView.OnItemClickListener


abstract void

onItemClick(AdapterView<?>parent, View view, int position, long id)

Callbackmethod to be invoked when an item in this AdapterView has been clicked.


AdapterView

public abstract class

AdapterView

extends ViewGroup

 

Object

getItemAtPosition(int position)

Gets the data associated with the specified position in the list.

 

 

Toast

public class

Toast

extends Object

 

Constants

int        LENGTH_LONG        Showthe view or text notification for a long period of time.

int        LENGTH_SHORT        Showthe view or text notification for a short period of time.

 

static Toast

makeText(Context context, int resId, int duration)

Make a standard toast that just contains a text view with the text from a resource.

 

static Toast

makeText(Context context, CharSequence text, int duration)

Make a standard toast that just contains a text view.

 

void

show()

Show the view for the specified duration.

 

用SimpleAdapter结合ListView进行显示

运行效果

 

 

源代码ActivityList2.java

 

package com.rainsong.listviewdemo;

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

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener;  
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class ActivityList2 extends Activity
{
    private List<Map<String, Object>> data;
    private ListView listView;
    private SimpleAdapter adapter;
    OnItemClickListener listener;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        data = new ArrayList<Map<String, Object>>();
        Map<String, Object> item;

        item = new HashMap<String, Object>();
        item.put("姓名", "张三");
        item.put("性别", "男");
        data.add(item);

        item = new HashMap<String, Object>();
        item.put("姓名", "李四");
        item.put("性别", "男");
        data.add(item);

        item = new HashMap<String, Object>();
        item.put("姓名", "王五");
        item.put("性别", "男");
        data.add(item);

        item = new HashMap<String, Object>();
        item.put("姓名", "小雪");
        item.put("性别", "女");
        data.add(item);

        item = new HashMap<String, Object>();
        item.put("姓名", "小明");
        item.put("性别", "男");
        data.add(item);

        listView = new ListView(this);
        adapter = new SimpleAdapter(this, data,
                android.R.layout.simple_list_item_2, new String[] {"姓名", "性别"},
                new int[] {android.R.id.text1, android.R.id.text2});
         listView.setAdapter(adapter);

         setContentView(listView);

         listener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent,
                    View view, int position, long id) {
                Toast.makeText(ActivityList2.this, parent.getItemAtPosition(position).toString()
                    + " clicked",Toast.LENGTH_SHORT).show();
            }
        };
        listView.setOnItemClickListener(listener);
    }
}

API知识点

SimpleAdapter

public class

SimpleAdapter

extends BaseAdapter

implements Filterable

 

SimpleAdapter(Contextcontext, List<? extends Map<String, ?>> data, int resource,String[] from, int[] to)

Constructor

 

 

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。