android UI控件之ListView详解之二listView+SimpleAdpater

SimpleAdpater是BaseAdpater,实现了BaseAdpater的四个抽象方法并进行了封装。也需要对数据进行适配。

构造方法:

Public SimpleAdpater(Contextcontext,List<? Extends Map>data,int resource,String[]from,int[] to);

Context context:Context上下文对象

List<? Extends Map>data:数据集合,data中的每一项对应着ListView中每一项数据。

int resource:Item布局的资源id

String[] from :map集合里面的key值。

Int[]  to:Item布局相应的控件id。

 代码MainActivity.java:

import android.annotation.SuppressLint;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.widget.ListView;import android.widget.SimpleAdapter;import java.util.ArrayList;import java.util.HashMap;import java.util.List;import java.util.Map;public class MainActivity extends AppCompatActivity {    private ListView LV;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        LV=(ListView)findViewById(R.id.LV);        //准备集合数据        List> data=new ArrayList>();        Map map=new HashMap();        map.put("icon",R.drawable.m1);        map.put("name","name--1");        map.put("context","context--1");        data.add(map);        map = new HashMap();        map.put("icon",R.drawable.m2);        map.put("name","name--2");        map.put("context","context--2");        data.add(map);        map = new HashMap();        map.put("icon",R.drawable.m3);        map.put("name","name--3");        map.put("context","context--3");        data.add(map);        map = new HashMap();        map.put("icon",R.drawable.m4);        map.put("name","name--4");        map.put("context","context--4");        data.add(map);        map = new HashMap();        map.put("icon",R.drawable.m5);        map.put("name","name--5");        map.put("context","context--5");        data.add(map);        map = new HashMap();        map.put("icon",R.drawable.m6);        map.put("name","name--6");        map.put("context","context--6");        data.add(map);        map = new HashMap();        map.put("icon",R.drawable.m7);        map.put("name","name--7");        map.put("context","context--7");        data.add(map);        //map对象中的key的数组, 用于得到对应的value        String [] from={"icon","name","context"};        //Item布局文件中的子view的id的数组,from...to..从哪对应到哪。一定是一一对应的        int[] to={R.id.icon,R.id.tv_name,R.id.tv_context};        //准备SimpleAdapter对象        SimpleAdapter simpleAdapter=new SimpleAdapter(this,data,R.layout.item_simple_adpater,from,to);        //设置Adapter显示列表        LV.setAdapter(simpleAdapter);    }}
item_simple_adpater.xml:
<?xml version="1.0" encoding="utf-8"?>     android:id="@+id/icon"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        app:srcCompat="@android:drawable/btn_star_big_on" />                android:id="@+id/tv_name"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="name"             />        android:id="@+id/tv_context"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="Context"            />    
main_activity.xml:
<?xml version="1.0" encoding="utf-8"?>    android:id="@+id/LV"//数据通过适配器,添加到Listview        android:layout_width="match_parent"        android:layout_height="match_parent"        >




更多相关文章

  1. Android数据持久化——五种方式
  2. Android开发资料集合
  3. Android的ListView数据更新后,如何使最新的条目可以自动滚动到可
  4. Android横竖屏加载不同布局的适配方案
  5. Android设备如何保证数据同步写入磁盘
  6. Android(安卓)SQLite操作类--封装
  7. Android(安卓)中数据库查询方法 query() 中的 selectionArgs 的
  8. Android原生项目引入最新的React Native包
  9. mybatisplus的坑 insert标签insert into select无参数问题的解决

随机推荐

  1. 用Fiddler抓取Android、Iphone网络数据包
  2. Android硬编码——音频编码、视频编码及
  3. android工程建立到最后一步提示unsupport
  4. Android外挂多字幕开源库(Subtitle for An
  5. Android之TextView属性列表
  6. Android中的adapter
  7. Erlang实现的百度云推送Android服务端实
  8. Android的selector,背景选择器
  9. C#/IOS/Android通用加密解密方法
  10. Android动画效果-更新中