zoukankan      html  css  js  c++  java
  • SimpleAdapter与listview,gridview的组合用法

    首先要明白SimpleAdapter构造方法的几个参数的含义:

    public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) 
    其中 context为上下文,一般为this.
    List为要绑定到该适配器上的数据内容,为list形式;
    resource为要绑定到该视图上的基本单元的layout.xml文件,from为该list下各个键值对中的"键"的名称,to为要绑定的基本单元layout文件
    中的各个组件的id,官方文档的解释如下:


    所以,要想使用SimpleAdapter,必须准备好这几个资源,基本单元的视图xml文件必须的,当然这个基本单元是为它的父容器准备的,父容器就是要放
    很多基本单元的小容器,所以呢,以MVC的观点来理解simpleadapter其实就是个C控制器,List这些就是模型M,视图就是由listveew或者其他的
    view来完成的,比如下面这个例子,就只是用了一个gridview,然后该gridview来放基本单元menu_item.xml,该基本单元由一个文字和图片组成
    一个组,一个组有即使用一个基本的视图文件.

    还是用例子来说你那个问题吧:本问题使用的是Listview:

    再用一个gridview来完成本次记录:

    基本单元的布局文件menu_item.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout_Item"
    android:layout_width="fill_parent" android:layout_height="wrap_content"
    android:paddingBottom="5dip">
    <ImageView android:id="@+id/item_image"
    android:layout_centerHorizontal="true" android:layout_width="wrap_content"
    android:layout_height="wrap_content"></ImageView>
    <TextView android:layout_below="@id/item_image" android:id="@+id/item_text"
    android:layout_centerHorizontal="true" android:layout_width="wrap_content"
    android:layout_height="wrap_content" android:text="选项"></TextView>
    </RelativeLayout>

    要显示的视图的主文件gridview(本例为一个popupwindow):
    popupwindow.xml
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <GridView
    android:id="@+id/gridview1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:numColumns="4"
    android:verticalSpacing="10dip"
    android:horizontalSpacing="10dip"
    android:stretchMode="columnWidth"
    android:gravity="center"
    />

    </LinearLayout>

    Java部分代码:
    GridView menuGrid=null;
    final String[] menu_name_array = { "收藏网址", "刷新页面", "日间模式", "夜间模式", "检查网络",
    "立即登录", "退出程序", "系统设置" };
    final int [] menu_image_array={R.drawable.star,R.drawable.star,R.drawable.star,R.drawable.star,R.drawable.star,
    R.drawable.star,R.drawable.star,R.drawable.star};
     
    final View contentview = LayoutInflater.from(MainActivity.this).inflate(R.layout.popupwindow, null);
    menuGrid = (GridView) contentview.findViewById(R.id.gridview1);
    menuGrid.setAdapter(getMenuAdapter(menu_name_array,menu_image_array));
    private SimpleAdapter getMenuAdapter(String[] menuNameArray,
    int[] imageResourceArray) {
    List<Map<String,Object>> data=new ArrayList<Map<String, Object>>();
    for (int i=0;i<imageResourceArray.length;i++){
    Map<String,Object> map=new HashMap<String, Object>();
    map.put("item_image",imageResourceArray[i]);
    map.put("item_text",menuNameArray[i]);
    data.add(map);
    }
    SimpleAdapter simpleAdapter=new SimpleAdapter(this,data,R.layout.item_menu,new String[]{"item_text","item_image"},new int[]{R.id.item_text,R.id.item_image});
    return simpleAdapter;
    }
     
     
     
  • 相关阅读:
    分布式锁原理及实现方式
    【FAQ】Maven 本地仓库明明有jar包,pom文件还是报错解决办法
    【FAQ】tomcat启动jdk版本不一致
    【Map,HashMap,Vector,List】资料汇总
    【FAQ】调用接口序列化问题
    【docker】docker下安装mysql
    linux tcpdump抓包Post请求
    Springboot 在@Configuration注解的勒种 使用@Autowired或者@value注解 读取.yml属性失败
    Springboot使用Shiro-整合Redis作为缓存 解决定时刷新问题
    CentOS yum 安装nginx
  • 原文地址:https://www.cnblogs.com/imqsl/p/6612541.html
Copyright © 2011-2022 走看看