分享好友 移动开发首页 频道列表

Android之SimpleAdapter简单实例和SimpleAdapter参数说明(zt)

Android开发  2016-12-01 14:280

http://blog.csdn.net/x605940745/article/details/11981049

SimpleAdapter的参数说明

第一个参数 表示访问整个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资源覆盖

<LinearLayout xmlns: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>  
<?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="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>  
package com.example.simpleadptertest;  
  
import java.util.ArrayList;  
import java.util.HashMap;  
import java.util.List;  
import java.util.Map;  
  
import android.app.Activity;  
import android.os.Bundle;  
import android.view.Menu;  
import android.widget.ListView;  
import android.widget.SimpleAdapter;  
  
public class MainActivity extends Activity {  
  
    private String[] name = { "剑萧舞蝶", "张三", "hello", "诗情画意" };  
  
    private String[] desc = { "魔域玩家", "百家执行", "高级的富一代", "妹子请过来..一个善于跑妹子的。。" };  
  
    private int[] imageids = { R.drawable.libai, R.drawable.nongyu,  
            R.drawable.qingzhao, R.drawable.tiger };  
      
    private ListView lt1;  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        List<Map<String, Object>> listems = new ArrayList<Map<String, Object>>();  
        for (int i = 0; i < name.length; i++) {  
            Map<String, Object> listem = new HashMap<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资源 
         * 下面的程序中如果 new String[] { "name", "head", "desc","name" } new int[] {R.id.name,R.id.head,R.id.desc,R.id.head} 
         * 这个head的组件会被name资源覆盖 
         * */  
        SimpleAdapter simplead = new SimpleAdapter(this, listems,  
                R.layout.simple_item, new String[] { "name", "head", "desc" },  
                new int[] {R.id.name,R.id.head,R.id.desc});  
          
        lt1=(ListView)findViewById(R.id.lt1);  
        lt1.setAdapter(simplead);  
          
    }  
  
    @Override  
    public boolean onCreateOptionsMenu(Menu menu) {  
        // Inflate the menu; this adds items to the action bar if it is present.  
        getMenuInflater().inflate(R.menu.main, menu);  
        return true;  
    }  
  
}  

Android之SimpleAdapter简单实例和SimpleAdapter参数说明(zt)

查看更多关于【Android开发】的文章

展开全文
相关推荐
反对 0
举报 0
评论 0
图文资讯
热门推荐
优选好物
更多热点专题
更多推荐文章
Supporting Multiple Screens
术语和概念Screen size 屏幕尺寸又称「屏幕大小」,是屏幕对角线的物理尺寸。单位英寸 inch,比如 Samsung Note4 是 5.7 英寸。Resolution 屏幕分辨率屏幕纵横方向上物理像素的总数,比如 Samsung Note4 是 2560x1440,表示纵向有 2560 个像素,横向有 1440

0评论2017-02-05363

Android插件化(4):OpenAtlasの插件的卸载与更新
如果看过我的前两篇博客Android插件化(2):OpenAtlas插件安装过程分析和Android插件化(3):OpenAtlas的插件重建以及使用时安装,就知道在插件的安装过程中OpenAtlas做了哪些事,那么插件的卸载就只需要把持久化和内存中的内容移除即可。1.插件的卸载插件卸载的

0评论2017-02-05229

个人简历
吴朝晖/男/1993.1本科/南京师范大学中北学院信息系工作年限:1年以内技术博客:wuzhaohui026.github.ioGitHub:https://github.com/wuzhaohui026期望职位:Android开发(初级Android工程师)期望薪资:税前月薪5.5k~7k期望城市:常州工作经历常州慧展信息科技有

0评论2017-02-05126

Android插件化(五):OpenAtlasの四大组件的Hack
引言到目前为止,我们已经分析了OpenAtlas中插件的安装,卸载,更新,以及安装好插件之后组件类的加载过程,但是对于这些是如何引发的还不知道,比如,在宿主的一个Activit中调用startActivity()跳转到插件中的一个Activity,如何判断这个Activity在的插件是否

0评论2017-02-0598

更多推荐