同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter,SimpleAdapter,SimpleCursorAdapter,以及重写BaseAdapter等方法。

  ArrayAdapter比较简单,但它只能用于显示文字。而SimpleAdapter则有很强的扩展性,可以自定义出各种效 果,SimpleCursorAdapter则可以从数据库中读取数据显示在列表上,通过从写BaseAdapter可以在列表上加处理的事件等。

  ArrayAdapter:

 package com.shang.test;

  import java.util.ArrayList;

  import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView; /**
*
* @author shangzhenxiang
*
*/
public class TestArrayAdapterActivity extends Activity{ private ListView mListView;
private ArrayList<String> mArrayList = new ArrayList<String>(); @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testarrayadapter);
mListView = (ListView) findViewById(R.id.myArrayList);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, getData()));
} private ArrayList<String> getData() {
mArrayList.add("测试数据1");
mArrayList.add("测试数据2");
mArrayList.add("测试数据3");
mArrayList.add("测试数据4");
mArrayList.add("测试数据5");
mArrayList.add("测试数据6");
return mArrayList;
}
}

View Cod

布局里面有个ListView就可以了:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/hello"/>
<ListView
android:id="@+id/myArrayList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

上面的代码中用到了ArrayAdapter的构造方法:

public ArrayAdapter (Context context, int textViewResourceId, T[] objects)

Api中是这么描述的:

其中Context为当前的环境变量,可以显示一行文字的一个布局文件,和一个List的集合,也就是数据源。

布局文件可以自己写,也可以用系统的,我这里是用的系统的。自己写的布局中包含一个TextView就可以了,当然系统中也有包含一个TextView的布局文件:就是 android.R.layout.simple_expandable_list_item_1,调用这个比较方便。

这里给出运行后的效果图:

  


SimpleAdapter:

package com.shang.test;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List; import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter; /**
*
* @author shangzhenxiang
*
*/
public class TestSimpleAdapter extends Activity { private ListView mListView;
private SimpleAdapter mAdapter;
private List<HashMap<String, Object>> mHashMaps;
private HashMap<String, Object> map; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testsimpleadapter);
mListView = (ListView) findViewById(R.id.mySimpleList);
mAdapter = new SimpleAdapter(this, getData(), R.layout.simpleitem, new String[]{"image", "title", "info"}, new int[]{R.id.img, R.id.title, R.id.info});
mListView.setAdapter(mAdapter);
} private List<HashMap<String, Object>> getData() {
mHashMaps = new ArrayList<HashMap<String,Object>>();
map = new HashMap<String, Object>();
map.put("image", R.drawable.gallery_photo_1);
map.put("title", "G1");
map.put("info", "google 1");
mHashMaps.add(map); map = new HashMap<String, Object>();
map.put("image", R.drawable.gallery_photo_2);
map.put("title", "G2");
map.put("info", "google 2");
mHashMaps.add(map); map = new HashMap<String, Object>();
map.put("image", R.drawable.gallery_photo_3);
map.put("title", "G3");
map.put("info", "google 3"); mHashMaps.add(map);
return mHashMaps;
}
}
<?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="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:id="@+id/img"
android:layout_margin="5px"
android:layout_height="wrap_content">
</ImageView>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="22px"></TextView>
<TextView
android:id="@+id/info"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="13px"></TextView>
</LinearLayout>
</LinearLayout>

simpleAdapter的:

第一个参数和第三个参数跟ArrayAdapter中的是一样的,第二个参数就是由HashMap组成的List,也就是数据源,而第5个参数也就是map中的key,最后一个参数就是map中key对应的值要显示在布局中的位置的id。

看下效果:

各种Adapter的用法的更多相关文章

  1. 转:各种Adapter的用法

    各种Adapter的用法   同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter,SimpleAdapter,SimpleCursorAdapt ...

  2. 【转】Android各种Adapter的用法

    转自:http://my.oschina.net/u/658933/blog/372151 Android各种Adapter的用法 发表于5个月前(2015-01-27 10:56)   阅读(143 ...

  3. Android RecyclerView Adapter 新式用法之SortedListAdapterCallback

    引言 前几天在同事的提醒下发现V7中有了一个新的工具类SortedListAdapterCallback,配合RecyclerView Adapter和SortedList一起使用更加方便的管理我们在 ...

  4. Adapter基本用法

    使用流程 graph LR A(新建适配器) -->B(绑定数据源) B-->C(设置适配器) 1. ArrayAdapter new ArrayAdapter<?>(cont ...

  5. Android开发之 。。各种Adapter的用法

    同样是一个ListView,可以用不同的Adapter让它显示出来,比如说最常用的ArrayAdapter,SimpleAdapter,SimpleCursorAdapter,以及重写BaseAdap ...

  6. 【转载】Adapter用法总结大全

    下面的是看到的比较好的地址: Android各种Adapter的用法:                 http://my.oschina.net/u/658933/blog/372151 Andro ...

  7. Java设计模式之适配器模式(Adapter Pattern)

    Adapter Pattern的作用是在不改变功能的前提下转换接口.Adapter分为两类,一类是Object Adapter, 还有一类是Class Adapter.因为Class Adapter的 ...

  8. android开发文章收藏

    1.activity [Android的从零单排开发日记]之入门篇(四)——Android四大组件之Activity 两分钟彻底让你明白Android Activity生命周期(图文)! 2.serv ...

  9. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

随机推荐

  1. 修改MySQL默认最大连接数

    修改MySQL默认最大连接数 MYSQL数据库安装完成后,默认最大连接数是100,一般流量稍微大一点的论坛或网站这个连接数是远远不够的,增加默认MYSQL连接数的方法有两个: 方法一: 进入MYSQL ...

  2. [记录]关于vertical-align单/多选框与说明文字对齐效果

    效果图:    第一张使用label标签,第二张没有使用.. 使用label标签,middle对齐方式的单选框下降了1px 而没有使用label标签,sub对齐方式的 却 居 中 了 =_= 不太理解 ...

  3. HtmlHelp

    @Html.Label("Label", "Label") @*渲染成<label for="Label">Label</ ...

  4. dir_colors linux颜色配置

    在控制台下,用ls,就会发现,shell将不同类型的文件项目显示为不同的颜色.者可以提高效率,不用ls -l便能大概的把各个文件的类型情况了解一下. 你有没有想过更改这个着色配置呢? 其实,在/etc ...

  5. 使用bootstrap时,选项框出现的意外效果,怎么办?

    <label for="" style="float:right;margin-right:10%;">     <input type=&q ...

  6. [Head First Python]1. 初始python-人人都爱列表

    movies = [ "hello", "world",["xin","lover",["Jerry" ...

  7. python核心编程-第五章-个人笔记

    1.用del删除对对象的引用 >>> a = 123 >>> a 123 >>> del a >>> a Traceback ( ...

  8. 计算球体积,hdu-2002

    计算球体积 Problem Description 根据输入的半径值,计算球的体积.   Input 输入数据有多组,每组占一行,每行包括一个实数,表示球的半径.   Output 输出对应的球的体积 ...

  9. Spring中的DataBinding(一)

    DataBinding在Spring中应用. 第一点:使用ModelAttribute指定带绑定的数据内容 很重要的属性:@ModelAttribute([value=""])可以 ...

  10. 正式学习React( 三)

    最基本的jsx语法什么的,我就不介绍了,唯一觉得有用点的,就是声明周期了. 下面的内容是转来的,自己也可以网上去搜,我觉得别人归纳的挺不错的,不过写法可能不是es6的,不影响学习. 在组件的整个生命周 ...