Android之SimpleAdapter简单实例和SimpleAdapter参数说明(zt)
16lz
2022-05-21
Android之SimpleAdapter简单实例和SimpleAdapter参数说明(zt)
http://blog.csdn.net/x605940745/article/details/11981049SimpleAdapter的参数说明
第一个参数 表示访问整个android应用程序接口,基本上所有的组件都需要第二个参数表示生成一个Map(String ,Object)列表选项
第三个参数表示界面布局的id 表示该文件作为列表项的组件
第四个参数表示该Map对象的哪些key对应value来生成列表项
第五个参数表示来填充的组件 Map对象key对应的资源一依次填充组件 顺序有对应关系
注意的是map对象可以key可以找不到 但组件的必须要有资源填充 因为 找不到key也会返回null 其实就相当于给了一个null资源
下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head}
这个head的组件会被name资源覆盖
代码
[html] view plain copy- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal"
- tools:context=".MainActivity">
- <ListView
- android:id="@+id/lt1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content">
- </ListView>
- </LinearLayout>
[html] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="horizontal">
- <ImageView
- android:id="@+id/head"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:paddingLeft="10dp"/>
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:orientation="vertical">
- <TextView
- android:id="@+id/name"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="20dp"
- android:textColor="#f0f"
- android:paddingLeft="10dp"/>
- <TextView
- android:id="@+id/desc"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="14dp"
- android:paddingLeft="10dp"/>
- </LinearLayout>
- </LinearLayout>
[java] view plain copy
- packagecom.example.simpleadptertest;
- importjava.util.ArrayList;
- importjava.util.HashMap;
- importjava.util.List;
- importjava.util.Map;
- importandroid.app.Activity;
- importandroid.os.Bundle;
- importandroid.view.Menu;
- importandroid.widget.ListView;
- importandroid.widget.SimpleAdapter;
- publicclassMainActivityextendsActivity{
- privateString[]name={"剑萧舞蝶","张三","hello","诗情画意"};
- privateString[]desc={"魔域玩家","百家执行","高级的富一代","妹子请过来..一个善于跑妹子的。。"};
- privateint[]imageids={R.drawable.libai,R.drawable.nongyu,
- R.drawable.qingzhao,R.drawable.tiger};
- privateListViewlt1;
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- List<Map<String,Object>>listems=newArrayList<Map<String,Object>>();
- for(inti=0;i<name.length;i++){
- Map<String,Object>listem=newHashMap<String,Object>();
- listem.put("head",imageids[i]);
- listem.put("name",name[i]);
- listem.put("desc",desc[i]);
- listems.add(listem);
- }
- /*SimpleAdapter的参数说明
- *第一个参数表示访问整个android应用程序接口,基本上所有的组件都需要
- *第二个参数表示生成一个Map(String,Object)列表选项
- *第三个参数表示界面布局的id表示该文件作为列表项的组件
- *第四个参数表示该Map对象的哪些key对应value来生成列表项
- *第五个参数表示来填充的组件Map对象key对应的资源一依次填充组件顺序有对应关系
- *注意的是map对象可以key可以找不到但组件的必须要有资源填充因为找不到key也会返回null其实就相当于给了一个null资源
- *下面的程序中如果newString[]{"name","head","desc","name"}newint[]{R.id.name,R.id.head,R.id.desc,R.id.head}
- *这个head的组件会被name资源覆盖
- **/
- SimpleAdaptersimplead=newSimpleAdapter(this,listems,
- R.layout.simple_item,newString[]{"name","head","desc"},
- newint[]{R.id.name,R.id.head,R.id.desc});
- lt1=(ListView)findViewById(R.id.lt1);
- lt1.setAdapter(simplead);
- }
- @Override
- publicbooleanonCreateOptionsMenu(Menumenu){
- //Inflatethemenu;thisaddsitemstotheactionbarifitispresent.
- getMenuInflater().inflate(R.menu.main,menu);
- returntrue;
- }
- }
更多相关文章
- Android四大组件之~~Service
- Android七大布局
- Android(安卓)NDK开发之旅38--FFmpeg视频添加水印
- Android(安卓)CountDownTimer实现定时器和倒计时效果
- Android(安卓)中文API (61) ―― ViewSwitcher
- TextView 限制最大行数、最小行数、字数超过“...”表示
- 关于Android中的radioGroup选择
- mybatisplus的坑 insert标签insert into select无参数问题的解决
- Python技巧匿名函数、回调函数和高阶函数