首先是创建三个全局变量:

SimpleAdapter listItemAdapter;  // ListView的适配器
ArrayList> listItem;  // ListView的数据源,这里是一个HashMap的列表
ListView myList;  // ListView控件

然后在Activity的onCreate函数中对变量进行初始化:

listItem = new ArrayList>();
listItemAdapter = new SimpleAdapter(this, listItem, R.layout.mylayout, 
new String[]{"image", "title", "text"},
new int[]{R.id.ItemImage, R.id.ItemTitle, R.id.ItemText});
myList = (ListView)findViewById(R.id.TaxiList);
myList.setAdapter(listItemAdapter);

添加两个私有的功能函数:
private void addItem()
{
HashMap map = new HashMap();
map.put("image", R.drawable.icon);
map.put("title", "标题");
map.put("text", "要显示的内容");
listItem.add(map);
listItemAdapter.notifyDataSetChanged();
}


private void deleteItem()
{
int size = listItem.size();
if( size > 0 )
{
listItem.remove(listItem.size() - 1);
listItemAdapter.notifyDataSetChanged();
}
}


另附上ListView的项自定义的Layout不再多说:
<?xml version="1.0" encoding="utf-8"?>  
android:id="@+id/RelativeLayout01"   
android:layout_width="fill_parent"   
xmlns:android="http://schemas.android.com/apk/res/android"   
android:layout_height="wrap_content"   
android:paddingBottom="4dip"   
android:paddingLeft="12dip"  
android:paddingRight="12dip">  
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:src="@drawable/taxi1" 
android:id="@+id/ItemImage" 
android:paddingTop="4dip">

android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:text="DaZhong Taxi Corporation" 
android:layout_toRightOf="@+id/ItemImage" 
android:id="@+id/ItemTitle" 
android:textSize="24dip">
android:layout_height="wrap_content" 
android:layout_width="fill_parent" 
android:text="Tel:021-67786874" 
android:id="@+id/ItemText" 
android:layout_below="@+id/ItemTitle" 
android:layout_toRightOf="@+id/ItemImage">

 

http://hi.baidu.com/relayon/blog/item/cc46a6259129506c34a80fb8.html

更多相关文章

  1. C语言函数的递归(上)
  2. Android(安卓)Audio代码分析3 - 创建AudioTrack对象
  3. 关于android的ListView优化
  4. Android开发之ListView 适配器(Adapter)优化
  5. Android(安卓)display架构分析(四)
  6. android浏览器研究-下载
  7. Service Manager进程从睡眠中被唤醒,返回BR_TRANSACTION,执行真正
  8. android 源码分析
  9. Android(安卓)Service详解(二)第一个Service

随机推荐

  1. 全面演示: 函数的作用域与闭包,回调的使
  2. 国际域名争议,仲裁机构VS法院,谁说的算?
  3. 第1章 0125-php数据类型与检测,学习心得、
  4. 1208作业
  5. 实用的php字符串函数总结
  6. PHP常用的字符串处理函数
  7. Python学习系列之 xrange和range的区别!
  8. PHP类与对象的基础概念
  9. DNS
  10. Java基础知识点1