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;
}
SimpleAdapter与listview,gridview的组合用法的更多相关文章
- 与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom
[源码下载] 与众不同 windows phone (52) - 8.1 新增控件: AutoSuggestBox, ListView, GridView, SemanticZoom 作者:webab ...
- ListView控件的用法
listView是一个可以用来显示视图列表的控件. 它使用适配器来为之提供数据和资源. ListView使用的基本步骤 得到ListView类型的对象mListView 生成适配器对象mListVie ...
- Android 中万能的 BaseAdapter(Spinner,ListView,GridView) 的使用!
大家好!今天给大家讲解一下BaseAdapter(基础适配器)的用法,适配器的作用主要是用来给诸如(Spinner,ListView,GridView)来填充数据的.而(Spinner,ListVie ...
- ScrollView嵌套ListView,GridView数据加载不全问题的解决
我们大家都知道ListView,GridView加载数据项,如果数据项过多时,就会显示滚动条.ScrollView组件里面只能包含一个组件,当ScrollView里面嵌套listView,GridVi ...
- 浅谈RecyclerView(完美替代ListView,GridView)
Android RecyclerView 是Android5.0推出来的,导入support-v7包即可使用. 个人体验来说,RecyclerView绝对是一款功能强大的控件. 首先总结下Recycl ...
- 使用自定的Adapter绑定ListView/GridView数据
使用自定的Adapter绑定ListView/GridView数据(地址) 对于ListView/Gridview的数据绑定, google提供了一些Adapter的模板, 自己也可以自定义一些个性化 ...
- Android之ListView/GridView 优化
一.效率最低的getView实现 我们知道,ListView和GridView的显示都是通过Adapter的getView实现的. ListView/GridView数据量较小时,我们的处理方式一般是 ...
- Android 快速开发系列 打造万能的ListView GridView 适配器
转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38902805 ,本文出自[张鸿洋的博客] 1.概述 相信做Android开发的写 ...
- universal image loader在listview/gridview中滚动时重复加载图片的问题及解决方法
在listview/gridview中使用UIL来display每个item的图片,当图片数量较多需要滑动滚动时会出现卡顿,而且加载过的图片再次上翻后依然会重复加载(显示设置好的加载中图片) 最近在使 ...
随机推荐
- (数组)Largest Rectangle in Histogram(栈解问题)
Largest Rectangle in Histogram Given n non-negative integers representing the histogram's bar height ...
- GTXE_COMMON
http://forums.xilinx.com/xlnx/board/crawl_message?board.id=IMPBD&message.id=9657 If you are usin ...
- TinkerPop中的遍历:图的遍历中谓词、栅栏、范围和Lambda的说明
关于谓词的注意事项 P是Function<Object,Boolean>形式的谓词.也就是说,给定一些对象,返回true或false.所提供的谓词在下表中概述,并用于各种步骤,例如has( ...
- 《Head First Servlets & JSP》-13-过滤器和包装器
过滤器是什么 与servlet非常类似,过滤器就是java组件,请求发送到servlet之前,可以用过滤器截获和处理清求,另外 servlet结束工作之后,在响应发回给客户之前,可以用过滤器处理响应. ...
- txt中把换行替换为空格
把合适改为html后打开,换行都没了,然后复制到另一个txt即可
- hadoop streaming 文档
Hadoop Streaming框架使用(一) Streaming简介 Streaming框架允许任何程序语言实现的程序在Hadoop MapReduce中使用,方便已有程序向Hadoop平台移植.因 ...
- Java从入门到放弃——05.修饰符static,final,权限修饰符
本文目标 static final: 权限修饰符:public,private,protected,缺省 1.static 静态修饰符,被static修饰的变量或者方法会被加载进静态区内存,不需要创建 ...
- linux进程池模型
static int nchildren;static pid_t* pids;int main(int argc,char**argv){ int listenfd,i; socklen_t add ...
- 19.Longest Substring Without Repeating Characters(长度最长的不重复子串)
Level: Medium 题目描述: Given a string, find the length of the longest substring without repeating cha ...
- kuangbin专题十六 KMP&&扩展KMP HDU1686 Oulipo
The French author Georges Perec (1936–1982) once wrote a book, La disparition, without the letter 'e ...
所以,要想使用SimpleAdapter,必须准备好这几个资源,基本单元的视图xml文件必须的,当然这个基本单元是为它的父容器准备的,父容器就是要放
