正常访问状态! 设为首页 | 加入收藏夹 | 浏览历史  
  http://www.guosp.com
 碧海澜涛居
  海纳百川,有容乃大。壁立千刃,无欲则刚!
 
 
关键词:
  网站首页 | 关于本站 | 技术资料 | 美文日志 | 读书收藏 | 影视收藏 | 软件收藏 | 摄影相册| 留言板 
  技术资料 >> Android技术 关闭(快捷键alt+C)
搜索标签: 暂无标签
android之SimpleAdapter创建和动态添加.删除SimpleAdapter选中项
[阅读次数:796次]  [发布时间:2013年12月16日]

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" />

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. <ImageView
  7. android:src="@drawable/ic_launcher"
  8. android:layout_width="48dp"
  9. android:layout_height="48dp"
  10. />
  11. <TableLayout
  12. android:layout_width="0dp"
  13. android:layout_height="wrap_content"
  14. android:layout_weight="1.0"
  15. android:layout_gravity="center_vertical"
  16. android:stretchColumns="0,1"
  17. >
  18. <TableRow >
  19. <TextView android:id="@+id/tv_01"/>
  20. <TextView android:id="@+id/tv_02"/>
  21. </TableRow>
  22. <TableRow >
  23. <TextView android:id="@+id/tv_03"/>
  24. <TextView android:id="@+id/tv_04"/>
  25. </TableRow>
  26. </TableLayout>
  27. </LinearLayout>

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

  1. Android中动态添加和删除SimpleAdapter中项

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

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

    然后在Activity的onCreate函数中对变量进行初始化:
    1. listItem = new ArrayList<HashMap<String, Object>>();
    2. listItemAdapter = new SimpleAdapter(this, listItem, R.layout.mylayout,
    3. new String[]{"image", "title", "text"},
    4. new int[]{R.id.ItemImage, R.id.ItemTitle, R.id.ItemText});
    5. myList = (ListView)findViewById(R.id.TaxiList);
    6. myList.setAdapter(listItemAdapter);



    添加两个私有的功能函数:
    1. private void addItem()
    2. {
    3. HashMap<String, Object> map = new HashMap<String, Object>();
    4. map.put("image", R.drawable.icon);
    5. map.put("title", "标题");
    6. map.put("text", "要显示的内容");
    7. listItem.add(map);
    8. listItemAdapter.notifyDataSetChanged();
    9. }
    10. private void deleteItem()
    11. {
    12. int size = listItem.size();
    13. if( size > 0 )
    14. {
    15. listItem.remove(listItem.size() - 1);
    16. listItemAdapter.notifyDataSetChanged();
    17. }
    18. }


    另附上ListView的项自定义的Layout不再多说:

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <RelativeLayout
    3. android:id="@+id/RelativeLayout01"
    4. android:layout_width="fill_parent"
    5. xmlns:android="http://schemas.android.com/apk/res/android"
    6. android:layout_height="wrap_content"
    7. android:paddingBottom="4dip"
    8. android:paddingLeft="12dip"
    9. android:paddingRight="12dip">
    10. <ImageView
    11. android:layout_width="wrap_content"
    12. android:layout_height="wrap_content"
    13. android:src="@drawable/taxi1"
    14. android:id="@+id/ItemImage"
    15. android:paddingTop="4dip">
    16. </ImageView>
    17. <TextView
    18. android:layout_height="wrap_content"
    19. android:layout_width="fill_parent"
    20. android:text="DaZhong Taxi Corporation"
    21. android:layout_toRightOf="@+id/ItemImage"
    22. android:id="@+id/ItemTitle"
    23. android:textSize="24dip"></TextView>
    24. <TextView
    25. android:layout_height="wrap_content"
    26. android:layout_width="fill_parent"
    27. android:text="Tel:021-67786874"
    28. android:id="@+id/ItemText"
    29. android:layout_below="@+id/ItemTitle"
    30. android:layout_toRightOf="@+id/ItemImage">
    31. </TextView>
    32. </RelativeLayout>

    注:使用ArrayAdapter在动态更改内容时,不需要其他更改,但要使之生效,必须放在函数
    1. runOnUiThread(new Runnable(){
    2. public void run(){
    3. //此处为你要对ArrayAdapter内容更改所需要做的操作
    4. }
    5. });


    中进行操作才能生效。
    但使用SimpleAdapter进行更改之后,必须使用.notifyDataSetChanged();方法进行通知该SimpleAdapter内容已经发生改变。




本页地址: [复制地址]
该页内容非本站原创 收藏自:http://blog.csdn.net/heng615975867/article/details/8743557
返回顶部 关闭(快捷键alt+C)
评论统计(0条)| 我要评论
暂无评论内容!
我要评论 
我要评论: 带*部分需要填写
 姓名称呼: * 请填写您的姓名或呢称
联系方式: QQ,MSN,Email都可以,方便交流 (仅管理员可见)
 评论内容: * 不超过100字符,50汉字
验证码:
    
  推荐链接
·Android中ListView分页加载...
·android中使用httpclient提...
·Android的常见错误及解决办...
·android 文件上传(模拟表单...
·Google Maps Android API V...
·教你轻松显示Gif图片
·Android 自定义类库打包jar...
·Android HttpClient上传文件...
·Google Maps Android API V...
·Android上传文件到服务器
  最近更新  
·Host 'XXX' is not allowed...
·Win2008或IIS7的文件上传大...
·IIS7.0上传文件限制的解决方...
·测试信息2015-03-11
·asp.net中处理图片
·ASP.NET之Web打印-终极解决...
·Asp.net下C#调用Word模版实...
·asp.net下将页面内容导入到...
·asp.net导出为pdf文件
·asp.net生成pdf文件
·FCKeditor 文本编辑器的使用...
·ASP.NET 将数据生成PDF
·asp.net2.0导出pdf文件完美...
·AspJpeg的安装与测试
·JS验证浏览器版本对IE11的支...
  热门浏览  
·IE8和IE9出现“此网页上的问...
·无线路由器密码破解,教你断...
·js替换所有回车换行符
·QQ/MSN在线交流代码
·IE弹出“中国工商银行防钓鱼...
·如何取消键盘上的一些快捷键...
·win7声音小的解决方法
·webdav漏洞的利用
·强制两端对齐的函数或者CSS...
·win7下成功安装sql server ...
·显示器分辨率调的过高导致电...
·天诺时空技术技术论坛
·js验证手机号码格式
·JS展开和收缩效果(二)
·本地计算机上的 MSSQLSERVE...
  碧海澜涛居
网站首页关于本站站长简介开发案例技术资料美文日志摄影相册读书收藏影视收藏留言板
版权所有:碧海澜涛 QQ:410436434 Email:shaopo_guo@163.com 苏ICP备15000526号
免责声明:本站为个人网站,站内所有文字、图片等各类资料均为个人兴趣爱好所收集,不用作任何商业用途,亦不保证资料的真实性,若有因浏览本站内容而导致的各类纠纷,本站也不承担任何责任。本站部分内容来自互联网,如有涉及到您的权益或隐私请联系站长解决。