main.xml
- <?xml version="1.0" encoding="utf-8"?>
- <ListView xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/listView"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" />
item.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="56dp"
- >
- <ImageView
- android:src="@drawable/ic_launcher"
- android:layout_width="48dp"
- android:layout_height="48dp"
- />
- <TableLayout
- android:layout_width="0dp"
- android:layout_height="wrap_content"
- android:layout_weight="1.0"
- android:layout_gravity="center_vertical"
- android:stretchColumns="0,1"
- >
- <TableRow >
- <TextView android:id="@+id/tv_01"/>
- <TextView android:id="@+id/tv_02"/>
- </TableRow>
- <TableRow >
- <TextView android:id="@+id/tv_03"/>
- <TextView android:id="@+id/tv_04"/>
- </TableRow>
- </TableLayout>
- </LinearLayout>
activity
- package com.ghg.SimpleAdapter;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Random;
- import android.app.Activity;
- import android.os.Bundle;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- public class Day0604_SimpleAdapterActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- initView();
- }
- //构造数据源,一个存放map集合的ArrayList
- ArrayList<HashMap<String, Object>> list;
- private ArrayList<HashMap<String, Object>> getList(){
- list=new ArrayList<HashMap<String,Object>>();
- for(int i=1;i<=20;i++){
- HashMap<String, Object> map=new HashMap<String, Object>();
- //姓名,身高(cm),体重(斤),价格(万)
- map.put("name", "jiajia"+i);
- map.put("height", 160+new Random().nextInt(10));
- map.put("weight", 100+new Random().nextInt(10));
- map.put("price", 100+new Random().nextInt(200));
- list.add(map);
- }
- return list;
- }
- ListView listView;
- private void initView() {
- // TODO Auto-generated method stub
- listView=(ListView) findViewById(R.id.listView);
- String[] from={"name","height","weight","price"};
- int[] to={R.id.tv_01,R.id.tv_02,R.id.tv_03,R.id.tv_04};
- SimpleAdapter adapter=new SimpleAdapter(this, getList(), R.layout.item, from, to);
- listView.setAdapter(adapter);
- }
- }
-
Android中动态添加和删除SimpleAdapter中项
首先是创建三个全局变量:
SimpleAdapter listItemAdapter; // ListView的适配器
ArrayList<HashMap<String, Object>> listItem; // ListView的数据源,这里是一个HashMap的列表
ListView myList; // ListView控件
然后在Activity的onCreate函数中对变量进行初始化:
listItem = new ArrayList<HashMap<String, Object>>(); 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<String, Object> map = new HashMap<String, Object>(); 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"?> <RelativeLayout 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"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/taxi1" android:id="@+id/ItemImage" android:paddingTop="4dip"> </ImageView> <TextView 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"></TextView> <TextView 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"> </TextView> </RelativeLayout>
注:使用ArrayAdapter在动态更改内容时,不需要其他更改,但要使之生效,必须放在函数runOnUiThread(new Runnable(){ public void run(){ //此处为你要对ArrayAdapter内容更改所需要做的操作 } });
中进行操作才能生效。但使用SimpleAdapter进行更改之后,必须使用.notifyDataSetChanged();方法进行通知该SimpleAdapter内容已经发生改变。