第二章实例:SimpleAdapter结合listview实现列表视图
2024-10-18 03:31:53
package test.simpleAdapter; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import com.example.simpleadapters.R; import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class ListContent extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.list_content);
//-----------------------------------------
//为simpleAdapter构造数据
String []title = {"排山倒海","葵花点穴手","如来神掌"};
String []tips = {"威力巨大给对手造成巨大内伤,即使对手逃逸也会无疾而终","短暂制瘫对方,瞬间废除对手活动能力,即可为所欲为","有效杀伤力达几百米寸草不生,但灰尘太大不环保"};
int []icon = {R.drawable.psdh,R.drawable.khdxs,R.drawable.rlsz};
String []time ={"14:30","14:20","14:10"};
//---------------------------------------
//构造玩上述数据后,开始构造它们xml布局
//---------------------------------------
//将数据打包成MAP对象
List<Map<String,Object>> map_content = new ArrayList<Map<String,Object>>();
for(int i = 0; i < title.length;i++){
Map map = new HashMap<String, Object>();
//打包方式 key value键值对形式
map.put("title", title[i]);
map.put("tips", tips[i]);
map.put("icon", icon[i]);
map.put("time", time[i]);
//------------------------------------
map_content.add(map);
}
//----------------------------------------
//生成xml布局资源ID引用 int []xmlres = {R.id.avatar,R.id.list_title,R.id.list_tips,R.id.list_time};
//生成键素组
String []mapkey = {"icon","title","tips","time"};//与上面的xmlres数组的资源ID对应
//map对象构造完毕,接下来生成simpleAdapter对象
//构造simpleAdapter所需参数说明:context窗口对象,盛装map的list,列表栏目布局资源ID的引用,list中的map中对应的键名称,键名称对应的控件资源ID;
SimpleAdapter sAdapter = new SimpleAdapter(ListContent.this, map_content, R.layout.simple_adapter_list, mapkey, xmlres);
ListView list = (ListView)findViewById(R.id.main_list);
list.setAdapter(sAdapter);//设置资源 } }
xml:
//list_content
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"> </ListView> </LinearLayout>
//simple_adapter
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/avatar"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_weight="0.7"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="@+id/list_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.2"/>
<TextView
android:id="@+id/list_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.8"/>
</LinearLayout>
<TextView
android:id="@+id/list_tips"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout> </LinearLayout>
最新文章
- iOS 常用三方类库整理
- 【原】iphone6来了,我该做点什么(兼容iphone6的方法)
- vld使用
- 【ZJOI2004】嗅探器
- [Aaronyang] 写给自己的WPF4.5 笔记22 [3d交互与动画 3/4]
- Quartz.Net 配置模板范例
- 浅谈C#随机数发生器
- 微软下一代云环境Web开发框架ASP.NET vNext预览
- 转: cmd和amd的区别
- C++中的向上类型转换和向下类型转换
- Mac环境下.Net开发
- Elasticsearch安装详解
- MQTT客户端库-Paho GO
- 关于MarkDown里的图片问题
- CNUTCon2017全球运维技术大会(持续更新中) - 斯达克学院 - 实战驱动的 IT 教育平台 - Powered By EduSoho
- 0x01 Spring Cloud 概述
- [Canvas]更多的球
- Linux 使用系统ISO制作yum源
- Google.ProtocolBuffers.dll 之.Net应用(一)
- node(4)express 框架 EJS模板,cookie, session的学习