android之SimpleAdapter创建和动态添加.删除SimpleAdapter选中项



main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ListView xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/listView"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical" />  
  7.   
  8.      

item.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="56dp"  
  5.   >  
  6.   
  7.     <ImageView  
  8.         android:src="@drawable/ic_launcher"  
  9.         android:layout_width="48dp"  
  10.         android:layout_height="48dp"  
  11.        />  
  12.     <TableLayout   
  13.         android:layout_width="0dp"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_weight="1.0"  
  16.         android:layout_gravity="center_vertical"  
  17.         android:stretchColumns="0,1"  
  18.         >  
  19.         <TableRow >  
  20.             <TextView android:id="@+id/tv_01"/>  
  21.             <TextView android:id="@+id/tv_02"/>  
  22.         </TableRow>  
  23.         <TableRow >  
  24.             <TextView android:id="@+id/tv_03"/>  
  25.             <TextView android:id="@+id/tv_04"/>  
  26.         </TableRow>  
  27.           
  28.     </TableLayout>  
  29.   
  30. </LinearLayout>  

activity
  1. package com.ghg.SimpleAdapter;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.HashMap;  
  5. import java.util.Random;  
  6.   
  7. import android.app.Activity;  
  8. import android.os.Bundle;  
  9. import android.widget.ListView;  
  10. import android.widget.SimpleAdapter;  
  11.   
  12. public class Day0604_SimpleAdapterActivity extends Activity {  
  13.     /** Called when the activity is first created. */  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         initView();  
  19.     }  
  20.     //构造数据源,一个存放map集合的ArrayList  
  21.     ArrayList<HashMap<String, Object>> list;  
  22.     private   ArrayList<HashMap<String, Object>> getList(){  
  23.         list=new ArrayList<HashMap<String,Object>>();  
  24.         for(int i=1;i<=20;i++){  
  25.             HashMap<String, Object> map=new HashMap<String, Object>();  
  26.             //姓名,身高(cm),体重(斤),价格(万)  
  27.             map.put("name", "jiajia"+i);  
  28.             map.put("height", 160+new Random().nextInt(10));  
  29.             map.put("weight", 100+new Random().nextInt(10));  
  30.             map.put("price", 100+new Random().nextInt(200));  
  31.             list.add(map);  
  32.         }  
  33.           
  34.         return list;  
  35.           
  36.     }  
  37.     ListView listView;  
  38.     private void initView() {  
  39.         // TODO Auto-generated method stub  
  40.         listView=(ListView) findViewById(R.id.listView);  
  41.         String[] from={"name","height","weight","price"};  
  42.         int[] to={R.id.tv_01,R.id.tv_02,R.id.tv_03,R.id.tv_04};  
  43.         SimpleAdapter adapter=new SimpleAdapter(this, getList(), R.layout.item, from, to);  
  44.         listView.setAdapter(adapter);  
  45.     }  

  1. 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内容已经发生改变。



  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值