Android中ListView动态添加删除项
16lz
2021-01-24
首先是创建三个全局变量: SimpleAdapter listItemAdapter; // ListView的适配器 ArrayList 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.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: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_height="wrap_content" android:src="@drawable/taxi1" android:id="@+id/ItemImage" android:paddingTop="4dip"> android:layout_width="fill_parent" android:text="DaZhong Taxi Corporation" android:layout_toRightOf="@+id/ItemImage" android:id="@+id/ItemTitle" android:textSize="24dip"> 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 |
更多相关文章
- C语言函数的递归(上)
- Android(安卓)Audio代码分析3 - 创建AudioTrack对象
- 关于android的ListView优化
- Android开发之ListView 适配器(Adapter)优化
- Android(安卓)display架构分析(四)
- android浏览器研究-下载
- Service Manager进程从睡眠中被唤醒,返回BR_TRANSACTION,执行真正
- android 源码分析
- Android(安卓)Service详解(二)第一个Service