android 71 ArrayAdapter和SimpleAdapter
Activity和item:
Activity:
<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"
tools:context=".MainActivity" > <ListView
android:id="@+id/lv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/> </RelativeLayout> item:
<?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="wrap_content"
android:orientation="horizontal" > <ImageView
android:id="@+id/iv_photo"
android:layout_width="40dp"
android:layout_height="40dp"
android:src="@drawable/photo3"
/>
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="名字"
android:textSize="22sp"
android:layout_gravity="center_vertical"
/> </LinearLayout>
java:
package com.itheima.arraysimple; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); String[] objects = new String[]{
"小志",
"小志的儿子",
"萌萌"
}; ListView lv = (ListView) findViewById(R.id.lv); //ArrayAdapter只能够处理一种数据类型String,做了高度的封装。
lv.setAdapter(new ArrayAdapter<String>(this, R.layout.item_listview, R.id.tv_name, objects)); //集合中每个元素都包含ListView条目需要的所有数据,该案例中每个条目需要一个字符串和一个整型,所以使用一个map来封装这两种数据
List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("photo", R.drawable.photo1);
map1.put("name", "小志的儿子");
data.add(map1); Map<String, Object> map2 = new HashMap<String, Object>();
map2.put("photo", R.drawable.photo2);
map2.put("name", "小志");
data.add(map2); Map<String, Object> map3 = new HashMap<String, Object>();
map3.put("photo", R.drawable.photo3);
map3.put("name", "赵帅哥");
data.add(map3); //SimpleAdapter,item_listview是item,
//new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}指定photo放入R.id.iv_photo组件,name放入R.id.tv_name组件
lv.setAdapter(new SimpleAdapter(this, data, R.layout.item_listview,
new String[]{"photo", "name"}, new int[]{R.id.iv_photo, R.id.tv_name}));
} }
android 71 ArrayAdapter和SimpleAdapter的更多相关文章
- android 适配器 ArrayAdapter,SimpleAdapter的学习
今天认真看了下android适配器,学习了下它的使用方法. 一,ArrayAdapter ArrayAdapter 比较简单,只可以存放一行文本信息.下面是简单的实现 private ListView ...
- Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段(转)
摘自:http://blog.csdn.net/shakespeare001/article/details/7926783 Adapter是连接后端数据和前端显示的适配器接口,是数据Data和UI( ...
- 使用具体解释及源代码解析Android中的Adapter、BaseAdapter、ArrayAdapter、SimpleAdapter和SimpleCursorAdapter
Adapter相当于一个数据源,能够给AdapterView提供数据.并依据数据创建相应的UI.能够通过调用AdapterView的setAdapter方法使得AdapterView将Adapter作 ...
- android ArrayAdapter BaseAdapter SimpleAdapter使用讲解
不是我针对谁,我只想针对新手玩家. 不清楚Adapter作用的可以看一下http://www.cnblogs.com/zhichaobouke/p/5798672.html (括号里的内容都是我主观添 ...
- [转]Android适配器之ArrayAdapter、SimpleAdapter和BaseAdapter的简单用法与有用代码片段
收藏ArrayAdapter.SimpleAdapter和BaseAdapter的一些简短代码片段,希望用时方便想起其用法. 1.ArrayAdapter 只可以简单的显示一行文本 代码片段: A ...
- 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理
在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...
- ArrayAdapter与SimpleAdapter的使用
在使用ListView中我们使用到adapter,android中为我们不仅提供了BaseAdapter类来让我们自定义自己的Adapter,还为我们提供了ArrayAdapter以及SimpleAd ...
- ArrayAdapter、SimpleAdapter简单用法
1. 使用流程 2. ArrayAdapter new ArrayAdapter<?>(context, textViewResourceId, objects) context:上下 ...
- ArrayAdapter、SimpleAdapter和BaseAdapter示例代码
import android.content.Context; import android.util.Pair; import android.view.View; import android.v ...
随机推荐
- Android引导界面
一.所需素材 很有必要整理一下,里面附带友盟的社会化分享组件,我就不去掉了. 二.代码 import com.umeng.update.UmengUpdateAgent; import a ...
- XMLHttpRequest2的进步之处
本文参考自:XMLHttpRequest2 新技巧 (重点保留demo,方便自己日后查阅) HTML5是现在web开发中的热点,虽然关于web app和local app一直有争论,但是从技术学习的角 ...
- Stanford CoreNLP--常量定义
在运行Stanford CoreNLP过程中会用到tokenize,pos等参数,这些以常量形式定义在edu.stanford.nlp.pipeline.Annotator中,具体如下: /** * ...
- Wild Words
poj1816:http://poj.org/problem?id=1816 题意:给你n个模板串,然后每个串除了字母,还有?或者*,?可以代替任何非空单个字符,*可以替代任何长度任何串,包括空字符串 ...
- [topcoder]TopographicalImage
BFS.这里用了queue,以及在数据结构里存了上一个的高度.也可以递归调用完成BFS,在调用之前做判断:http://community.topcoder.com/stat?c=problem_so ...
- ANDROID_MARS学习笔记_S05_004_过滤杂质,得到真正的加速度
一.简介 二.代码1.java (1)MainActivity.java import android.app.Activity; import android.content.Context; im ...
- delphi中EmbeddedWB网页html相互调用(二)
我们可以通过控件 EmbeddedWB_D5-D2010_Version_14.69.1 来响应html事件,还可以自定义html响应哪些html元素. 控件下载 点击下载 里面有demos文件夹大家 ...
- Oracle core05_事务和一致性
事务和一致性 oracle的redo和undo机制保证了数据库的ACID特性,以及高性能和可恢复特性. redo的数据是记录着数据块变更的顺序的正向数据流, commit时,保证redo同步持久化,保 ...
- 改善C#程序的50种方法
为什么程序已经可以正常工作了,我们还要改变它们呢?答案就是我们可以让它们变得更好.我们常常会改变所使用的工具或者语言,因为新的工具或者语言更富生产力.如果固守旧有的习惯,我们将得不到期望的结果.对于C ...
- POJ_2392_Space_Elevator_(动态规划,背包)
描述 http://poj.org/problem?id=2392 磊方块,每种方块有数量,高度,以及该种方块所能处在的最高高度.问最高磊多高? Space Elevator Time Limit: ...