安卓学习第12课——SimpleAdapter
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了安卓学习第12课——SimpleAdapter,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3319字,纯文字阅读大概需要5分钟。
内容图文
SimpleAdapter一点也不简单,他的参数就带了5个,天哪,晕了。。
今天学的这个适配器, public SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)
看这个大概明白,参数分别是第一个:表示访问整个android应用程序接口,基本上所有的组件都需要,一般都写this(改天研究一下),第二个应该是这个List对象名,第三个是list的id,第4,5指的分别是他的list里面的Map存的内容,String类型和其他类型(他的id)。
< RelativeLayout 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" > < ListView android:id ="@+id/mylist" android:layout_width ="match_parent" android:layout_height ="wrap_content" /> </ RelativeLayout >
<? xml version="1.0" encoding="utf-8" ?> < LinearLayout xmlns:android ="http://schemas.android.com/apk/res/android" android:orientation ="horizontal" android:layout_width ="match_parent" android:layout_height ="wrap_content" > <!-- 定义一个ImageView,用于作为列表项的一部分。 --> < ImageView android:id ="@+id/header" android:layout_width ="wrap_content" android:layout_height ="wrap_content" android:paddingLeft ="10dp" /> < LinearLayout android:orientation ="vertical" android:layout_width ="match_parent" android:layout_height ="wrap_content" > <!-- 定义一个TextView,用于作为列表项的一部分。 --> < 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,用于作为列表项的一部分。 --> < 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.simpleadapter; 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.widget.ListView; import android.widget.SimpleAdapter; public class MainActivity extends Activity { //在xml中创建一个listview,用于显示simpleAdapter的列表项private String[] names=new String[]{"虎头","弄玉","李清照","李白"}; private String[] descs=new String[]{"可爱的小孩","一个擅长音乐的女孩","一个擅长文学的女性","浪漫主义诗人"}; privateint[] imagesIds=newint[]{R.drawable.tiger,R.drawable.nongyu,R.drawable.qingzhao,R.drawable.libai}; @Override protectedvoid onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //创建一个List集合,集合元素师Map List<Map<String,Object>> listItems=new ArrayList<Map<String,Object>>(); for(int i=0;i<names.length;i++){ Map<String,Object> listItem=new HashMap<String,Object>(); listItem.put("header", imagesIds[i]); listItem.put("personname", names[i]); listItem.put("desc", descs[i]); listItems.add(listItem); } //创建一个SimpleAdapter SimpleAdapter simpleAdapter=new SimpleAdapter(this,listItems,R.layout.simple_item, new String[]{"personname","header","desc"},newint[]{R.id.name,R.id.header,R.id.desc}); ListView list=(ListView) findViewById(R.id.mylist); list.setAdapter(simpleAdapter); } }
我认为这个代码实现的过程是:创建一个listview,实现simpleAdapter列表项,在创建的simpleAdapter里面写下列表项内容。
备注:这个学习了SimpleAdapter,list,HashMap.
原文:http://www.cnblogs.com/Yvettey-me/p/3920903.html
内容总结
以上是互联网集市为您收集整理的安卓学习第12课——SimpleAdapter全部内容,希望文章能够帮你解决安卓学习第12课——SimpleAdapter所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。