使用simpleAdapter的数据用一般都是HashMap构成的List,list的每一节对应ListView的每一行。HashMap的每个键 值数据映射到布局文件中对应id的组件上。因为系统没有对应的布局文件可用,我们可以自己定义一个布局vlist.xml。下面做适配,new一个 SimpleAdapter参数一次是:this,布局文件(vlist.xml),HashMap的 title 和 info,img。布局文件的组件id,title,info,img。布局文件的各组件分别映射到HashMap的各元素上,完成适配。

---listviewitem.xml

<?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:layout_marginTop="5dp"
    android:orientation="horizontal" >

<ImageView
        android:id="@+id/img1"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_margin="5dp"
        android:src="@drawable/p1" />

<LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_weight="0.08"
        android:orientation="vertical" >

<TextView
            android:id="@+id/t1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView"
            android:textSize="16dp" />

<TextView
            android:id="@+id/t2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
    </LinearLayout>
<Button
        android:id="@+id/btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" />
</LinearLayout>

-----主xml文件

<?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/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >
    </ListView>

</LinearLayout>

-----java文件

@Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.listviewtest);
        
        ListView lv=(ListView)findViewById(R.id.listView1);
        
        //{{ 形成list集合
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("title", "G1d");
        map.put("info", "google 1");
        map.put("img", R.drawable.p1);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("title", "G2d");
        map.put("info", "google 2");
        map.put("img", R.drawable.p2);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("title", "G3d");
        map.put("info", "google 3");
        map.put("img", R.drawable.p3);
        list.add(map);
        //}}
        
        SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.listviewitem,
                new String[]{"title","info","img"},
                new int[]{R.id.t1,R.id.t2,R.id.img1});
        lv.setAdapter(adapter);
        
    }

ListView simpleAdapter的基本使用的更多相关文章

  1. Android UI:ListView -- SimpleAdapter

    SimpleAdapter是扩展性最好的适配器,可以定义各种你想要的布局,而且使用很方便. layout : <?xml version="1.0" encoding=&qu ...

  2. Android -- ListView(SimpleAdapter) 自定义适配器

    aaarticlea/jpeg;base64,/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBA ...

  3. android ListView SimpleAdapter 带图片

    main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xml ...

  4. Android ListView SimpleAdapter支持Bitmap类型图片显示

    // 处理simpleAdapter中包括bitmap类型 adapter.setViewBinder(new ViewBinder() { public boolean setViewValue(V ...

  5. ListView - SimpleAdapter 行间颜色交替(转)

    一.概述 通过扩展SimpleAdapter,来改变显示外观.因为要每行的显示颜色,首先要获得每行的View实例,然后调用setBackgroundColor函数设置. 二.实例 [效果] [代码片段 ...

  6. android xml解析添加到listview中的问题

    一个问题不知什么原因,代码: public class OtherActivity extends ListActivity { @Override protected void onCreate(B ...

  7. 安卓第六天笔记--ListView

    安卓第六天笔记--ListView 1.AdapteView AdapteView 继承ViewGroup它的本质是容器 AdapterView派生了3个子类: AbsListView AbsSpin ...

  8. 38.Android之ListView简单学习(一)

    android中ListView用的很普遍,今天来学习下,本篇主要以本地数据加载到listview,后面会学习从网络获取数据添加到listview. 首先改下布局文件: <?xml versio ...

  9. Android ListView内容变化后的动态刷新

    ListView内容变化后的动态刷新 基本知识点: 1.更新适配器Adapter数据源 2.调用适配器Adapter的刷新方法notifyDataSetChanged() 首先需要定义ListView ...

随机推荐

  1. 值得推荐的C/C++框架和库 very good

    [本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看看现有的轮子吧 值得学习的C语 ...

  2. Android源码之Matrix

    Matrix类在Android中主要用来进行矩阵变换,其可操作的对象包括图像.点阵.Vector(向量).矩形等. Matrix支持的变换类型主要有以下几种: 1.Translate:平移变换 2.R ...

  3. Redundant Call to Object.ToString()

    Redundant Call to Object.ToString() The + operator for string is overloaded to call String.Concat pa ...

  4. linxu命令小结

    1.设置root用户的密码 sudo passwd root 2.切换用户 su username su:switch user的简写 3.切换到超级用户 sudo su sudo (super us ...

  5. 【canvas】伸缩 / 剪裁 / 文本 / 阴影 / 填充图案 / 填充渐变

    <!DOCTYPE html> <html lang="zh-CN"> <head> <meta charset="utf-8& ...

  6. ImageSwitcher 右向左滑动的实现方式

    ImageSwitcher is;...is.setInAnimation(this, android.R.anim.slide_in_left);is.setOutAnimation(this, a ...

  7. 剪花布条 --HDOJ 2087

    剪花布条 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  8. Bzoj 2662: [BeiJing wc2012]冻结 dijkstra,堆,分层图,最短路

    2662: [BeiJing wc2012]冻结 Time Limit: 3 Sec  Memory Limit: 128 MBSubmit: 647  Solved: 348[Submit][Sta ...

  9. Android 播放视频并获取指定时间的帧画面

    最近做的项目要求既能播放视频(类似于视频播放器),又能每隔1s左右获取一帧视频画面,然后对图片进行处理,调查了一周,也被折磨了一周,总算找到了大致符合要求的方法.首先对调查过程中涉及到的方法进行简单介 ...

  10. C语言 处理文件

    刚学习C语言不久,运用C语言处理各种文件.这里列出,处理文件所需的大部分函数,已经整理的笔记,使用的注意事项.