对于android来说,数据的展示与存储是非常重要的,那么,展示相应的数据列表更是很重要,下面我对android中ListView用法简洁的记录一下,方便大家查阅:

ListView项(Item)的三种布局使用例子

  1. 自定义的布局,使用了相对布局(RelativeLayout,见list_item.xml),左侧一个图片,右侧上方是字体比较大的title,下方是字体稍小的description;
  2. Android自带的布局(simple_list_item_2),主要是一个垂直的LinearLayout,里面包含两个ID分别为text1、text2的TextView,这两个TextView的字体大小不一样,一个带textAppearanceLarge属性,另外一个带textAppearanceSmall属性;(如果只要显示一个内容就用simple_list_item_1)
  3. Android自带的布局(见源码里的two_line_list_item.xml),主要是一个垂直的LinearLayout,里面包含两个ID分别为text1、text2的TextView,这两个TextView的字体大小一样,但每个TextView还带一个字体加粗的属性。
  • 下面用一个例子来说明其中的用法区别
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="vertical"><ListView android:id="@+id/android:list"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_weight="1"android:drawSelectorOnTop="false"/><LinearLayoutandroid:layout_width="fill_parent"android:layout_height="wrap_content"android:orientation="horizontal"android:gravity="center_horizontal"><Button android:id="@+id/btnPrevious"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginRight="10dip"android:text="Previous"></Button><Button android:id="@+id/btnNext"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Next"></Button></LinearLayout></LinearLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:layout_width="fill_parent"  android:layout_height="wrap_content"><ImageView android:id="@+id/imageView"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerVertical="true"android:layout_marginLeft="5px"android:layout_marginRight="5px"  android:src="@drawable/tea_80x59"/><TextView android:id="@+id/titleTextView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_toRightOf="@id/imageView"android:textSize="22px"/><TextView android:id="@+id/descTextView"android:layout_width="fill_parent"android:layout_height="wrap_content"android:layout_toRightOf="@id/imageView"android:layout_below="@id/titleTextView"android:textSize="12px"/></RelativeLayout>
MySimpleAdapterActivity.java
package com.tutor.simpleadapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;import android.app.Activity;import android.app.ListActivity;import android.content.Intent;import android.os.Bundle;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ListView;import android.widget.SimpleAdapter;import android.widget.Toast;public class MySimpleAdapterActivity extends ListActivity {private Button btnPrevious = null;private Button btnNext = null;private String TAG = "SimpleAdapter";private int index = 0;    /** Called when the activity is first created. */    @Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);                btnPrevious = (Button)findViewById(R.id.btnPrevious);        btnNext = (Button)findViewById(R.id.btnNext);        btnPrevious.setOnClickListener(new ButtonClickListener(this));        btnNext.setOnClickListener(new ButtonClickListener(this));                Intent intent = getIntent();        index = intent.getIntExtra("Index", 0);        Log.d(TAG, "onCreate "+index);        switch((index % 3)) {        case 0://自定义的List Item布局        setListAdapter(new SimpleAdapter(this,         getData("custom-item"),         R.layout.list_item,             new String[]{"title", "description"},             new int[]{R.id.titleTextView, R.id.descTextView}));        Log.i(TAG, "SimpleAdapter with R.layout.list_1");        break;        case 1://Android提供的simple_list_item_2布局        setListAdapter(new SimpleAdapter(this,         getData("simple-list-item-2"),         android.R.layout.simple_list_item_2,             new String[]{"title", "description"},             new int[]{android.R.id.text1, android.R.id.text2}));        Log.i(TAG, "SimpleAdapter with android.R.layout.simple_list_item_2");        break;        case 2://Android提供的two_line_list_item布局        setListAdapter(new SimpleAdapter(this,         getData("two-line-list-item"),         android.R.layout.two_line_list_item,             new String[]{"title", "description"},             new int[]{android.R.id.text1, android.R.id.text2}));        Log.i(TAG, "SimpleAdapter with android.R.layout.two_line_list_item");        break;        default:        Log.i(TAG, "no SimpleAdapter");        }            }        protected void onRestart() {    super.onRestart();    Log.d(TAG, "onRestart "+index);    }        protected void onStart() {    super.onStart();    Log.d(TAG, "");    }        protected void onResume() {    super.onResume();    Log.d(TAG, "onResume "+index);    }        protected void onPause() {    super.onPause();    Log.d(TAG, "onPause "+index);    }        protected void onStop () {    super.onStop();    Log.d(TAG, "onStop "+index);    }        protected void onDestroy() {    Log.d(TAG, "onDestroy "+index);    if(btnPrevious != null) {    btnPrevious.setOnClickListener(null);    btnPrevious = null;    }        if(btnNext != null) {    btnNext.setOnClickListener(null);    btnNext = null;    }        super.onDestroy();    }        /**     * 构造SimpleAdapter的第二个参数,类型为List<Map<?,?>>     * @param title     * @return     */    private List<Map<String, String>> getData(String title) {    List<Map<String, String>> listData = new ArrayList<Map<String, String>>();    for(int i = 1; i<=10; i++) {    Map<String, String> map = new HashMap<String, String>();    map.put("title", title+" "+i);    map.put("description", "This is the description of "+title+" "+i);    listData.add(map);    }        return listData;    }        /**     * 当List的项被选中时触发     */    protected void onListItemClick(ListView listView, View v, int position, long id) {        Map map = (Map)listView.getItemAtPosition(position);        Toast toast = Toast.makeText(this, map.get("title")+" is selected.", Toast.LENGTH_LONG);        toast.show();    }        class ButtonClickListener implements View.OnClickListener  {    private Activity context = null;    public ButtonClickListener(Activity context) {    this.context = context;    }@Overridepublic void onClick(View v) {// TODO Auto-generated method stubif(context != null) {Intent intent = context.getIntent();int index = intent.getIntExtra("Index", 0);if(v.getId() == R.id.btnNext) {index += 1;}else if(v.getId() == R.id.btnPrevious) {index -= 1;if(index < 0) {index = Math.abs(index);}}//发起新的ActivityIntent newIntent = new Intent(context, MySimpleAdapterActivity.class);newIntent.putExtra("Index", index);context.startActivity(newIntent);//关闭原来的Activitycontext.finish();}}        }}
  • 下面两个是Android自带的布局,直接引用即可,不用添加到工程中:
base/core/res/res/layout/simple_list_item_2.xml
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android" android:paddingTop="2dip"android:paddingBottom="2dip"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:minHeight="?android:attr/listPreferredItemHeight"    android:mode="twoLine">    <TextView android:id="@android:id/text1"android:layout_width="match_parent"android:layout_height="wrap_content"        android:layout_marginLeft="6dip"        android:layout_marginTop="6dip"android:textAppearance="?android:attr/textAppearanceLarge"/><TextView android:id="@android:id/text2"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_below="@android:id/text1"        android:layout_alignLeft="@android:id/text1"android:textAppearance="?android:attr/textAppearanceSmall"/></TwoLineListItem>
base/core/res/res/layout/two_line_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="match_parent"    android:layout_height="wrap_content"    android:orientation="vertical">    <TextView android:id="@android:id/text1"        android:textSize="16sp"        android:textStyle="bold"        android:layout_width="match_parent"        android:layout_height="wrap_content"/>    <TextView android:id="@android:id/text2"        android:textSize="16sp"        android:layout_width="match_parent"        android:layout_height="wrap_content"/></LinearLayout>
三种不同方式展现结果如下:
android ListView用法简介

android ListView用法简介

android ListView用法简介
SimpleAdapter的使用: SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
  1. context:也就是上下文,而Activity都是继承于Context的,所以这个参数一般使用Activity的实例;
  2. data:类型要为一个List的子类,且List中的每个元素都必须为Map的子类,每个Map中以有一对或多对的Key-Value对,这些值与下面的参数from有对应;
  3. resource:这是每个List项(Item)对应的XML布局资源文件名,在里面控制List项显示的元素以及相对位置;
  4. from:是一个字符串数组,可以理解为列名,对应于data数据中Map元素的Key值,只有包含在这个字符串数组的元素对应到Map中的Value值才能显示到TextView中(这些TextView在最后一个参数to配置),所以这些Value值需要为字符串或者有toString()的方法;
  5. to:TextView组件ID的数组,上面的from参数配置了Key值,而根据这个Key值从data参数里的map取出的value值需要设置到TextView中,而这些TextView的id需要在这个数组中配置。而且这些TextView需要在参数resource对应的XML资源文件中配置。
ps: 例子中使用了ListActivity,它有默认的XML资源文件;但由于本例需要添加两个按钮(原来的布局是没有按钮),所以重新在main.xml中配置了一个ListView,且ListActivity要求放到它里面的ListView的ID必须是 android:list 。本例的两个按钮主要是切换不同的ListActivity来显示ListView项的三种不同布局,一种是自定义的,另外两种是Android自带的常用ListView项布局。

更多相关文章

  1. Android 中LayoutInflater(布局加载器)之实战篇
  2. 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、
  3. Android布局之相对布局——RelativeLayout
  4. 【Android 初学】3、控件布局初步
  5. Android ListView中点击单行实现RadioButton的单选功能,自定义Ite
  6. android五种布局-霓虹灯效果实现
  7. Android布局管理器-详细解析布局实现
  8. android主流UI布局
  9. Android布局管理器 - 详细解析布局实现

随机推荐

  1. Android测试教程(2):测试基础
  2. Android: AIDL --- Android中的远程接口
  3. Android的进程,线程模型
  4. Phonegap软键盘遮挡输入框问题
  5. Android(安卓)Activity的4种启动模式详解
  6. 漫谈Android安全框架
  7. 在Android程序中使用全局变量
  8. 从Android界面开发谈起
  9. Android(安卓)NDK环境搭建
  10. Android热点回顾第一期