ListView 是android开发中最常用的组件之一,它通过一个adapter来构建显示通常有三种adapter可以使用ArrayAdapter ,SimpleAdapter,CursorAdapter。CursorAdapter主要正对数据库使用,下面通过例子介绍ArrayAdapter ,SimpleAdapter的简单使用:
1:ArrayAdapter 它接受一个数组或者List作为参数来构建。
一下通过简单例子说明:
创建Test 继承ListActivity 这里我们传入一个string数组
public class ListTest extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] sw = new String[100];
for (int i = 0; i < 100; i++) {
sw[i] = "listtest_" + i;
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,sw);//使用系统已经实现好的xml文件simple_list_item_1
setListAdapter(adapter);
}
}
运行如图:
从以上代码可以看不我们不需要加载我们自己的layout 而是用系统已经实现的layout很快速的实现了listview
第二种SimpleAdapter:
先看下我们例子的最终截图:
通过上图可以看出listview每行不仅仅是一个string 包括了很多项,图片,多项文字
我们通过构建list,并设置每项为一个map来实现:
代码:创建TestList类继承Activity
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, Object>> users = new ArrayList<HashMap<String, Object>>();
for (int i = 0; i < 10; i++) {
HashMap<String, Object> user = new HashMap<String, Object>();
user.put("img", R.drawable.user);
user.put("username", "姓名(" + i+")");
user.put("age", (20 + i) + "");
users.add(user);
}
SimpleAdapter saImageItems = new SimpleAdapter(this,
users,// 数据来源
R.layout.user,//每一个user xml 相当ListView的一个组件
new String[] { "img", "username", "age" },
// 分别对应view 的id
new int[] { R.id.img, R.id.name, R.id.age });
// 获取listview
((ListView) findViewById(R.id.users)).setAdapter(saImageItems);
下面是main.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">
<TextView android:text="用户列表" android:gravity="center"
android:layout_height="wrap_content"
android:layout_width="fill_parent" android:background="#DAA520"
android:textColor="#000000">
</TextView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:text="姓名"
android:gravity="center" android:layout_width="160px"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#7CFC00">
</TextView>
<TextView android:text="年龄"
android:layout_width="170px" android:gravity="center"
android:layout_height="wrap_content" android:textStyle="bold"
android:background="#F0E68C">
</TextView>
</LinearLayout>
<ListView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/users">
</ListView>
</LinearLayout>
之中listView前面的可以说是标题行,listview相当于用来显示数据的容器,里面每行是一个用户信息,而用户信息是样子呢?
看看use.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
>
<TableRow >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/img">
</ImageView>
<TextView
android:layout_height="wrap_content"
android:layout_width="150px"
android:id="@+id/name">
</TextView>
<TextView
android:layout_height="wrap_content"
android:layout_width="170px"
android:id="@+id/age">
</TextView>
</TableRow>
</TableLayout>
也就是说每行包含了一个img 和2个文字信息
这个文件以参数的形式通过adapter在listview中显示。
也就是:
SimpleAdapter saImageItems = new SimpleAdapter(this,
users,// 数据来源
R.layout.user,//每一个user xml 相当ListView的一个组件
new String[] { "img", "username", "age" },
// 分别对应view 的id
new int[] { R.id.img, R.id.name, R.id.age });
分享到:
相关推荐
NULL 博文链接:https://aarongo.iteye.com/blog/1919251
ListView控件是最常用的控件之一,ListView的经常使用ArrayAdapter和SimpleAdapter来加载数据源。ArrayAdapter(数组适配器)用作展现简单的纯文字列表;SimpleAdapter(简单适配器)可以用来展现图文并茂的列表,...
ArrayAdapter和simpleAdapter的回顾,好长时间都没有写android了,今天出来溜溜,这个写的是最初的功能。 就是listview。simpleAdapter是左图片右文本、 这个适合初学者玩下。
Android中ListView常用的两种适配器ArrayAdapter和SimpleAdapter使用
NULL 博文链接:https://2528.iteye.com/blog/838329
android 的用arrayAdapter和SimpleAdapter的两种ListView 入门专用 不喜勿喷
ListView 与ArrayAdapter,BaseAdapter,SimpleAdapter,SimpleCursorAdapter配合使用实例。
Android源码:ListView组件应用演示代码,layout的xml文件名需要全部小写。 ActivityList1: 1.初步介绍ListView这个组件。 2.初步介绍ArrayAdapter。 # simple_list_item_single_choice # ...
ArrayAdapter和SimpleAdapter使用,详细了解请移步http://blog.csdn.net/zxc514257857/article/details/70217422
ListView加载ArrayAdapter和SimpleAdapter
根据列表的适配器类型,列表分为三种,ArrayAdapter,SimpleAdapter和SimpleCursorAdapter 其中以ArrayAdapter最为简单,只能展示一行字。SimpleAdapter有最好的扩充性,可以自定义出各种效果。SimpleCursorAdapter...
代码中主要介绍ListView的三种配置方法,包括ArrayAdapter,SimpleAdapter和BaseAdapter
演示了安卓中ArrayAdapter数组适配器和SimpleAdapter简单适配器的用法
简单的listview,有ArrayAdapter,SimpleAdapter和SimpleCursorAdapter。
ActivityList1:介绍ListView和ArrayAdapter组件。 ActivityList2:介绍cusor和CursorAdapter(有错误,没有调试成功) 1.ActivityList3:介绍SimpleAdapter、simple_list_item_1和simple_list_item_2并利用...
讲述四种适配器:ArrayAdapter SimpleAdapter SimpleCursorAdapter 自定义Adapter
android的UI设计基础教程,内容包括TextView,button,intent,Bundle,AlertDialog,menu,Toast,EditText,ImageButton,ImageView,Gallery,AutoCompleteTextView,ListView,ArrayAdapter,SimpleAdapter等
ListView基本使用,ArrayAdapter、SimpleAdapter、BaseAdapter
ListView之适配器的使用包含了ArrayAdapter,SimpleAdapter ,BaseAdapter等适配器。