在上一篇文章中,我们是直接将一个String的ArrayList传给ArrayAdapter,然后ArrayAdapter会将其中的字符串一个一个地放到对应item上的TextView中,最终展示出来。
但是当时我们用的是android系统中提供的布局文件,我们可以用自己提供的布局文件吗?当然是可以的。
如下,我们在layout文件中定义一个arrayadapter.xml,在里面放上一个TextView(这个TextView可是一定要的,如果我们要用来展示字符串的话),如下:
<?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" >
<TextView android:id="@+id/textView1"
android:drawableLeft="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
在TextView的旁边放了一个小图标,不过这样的话,在创建Adapter的时候,就必须用另外一个构造函数了,因为要告诉ArrayAdapter,要用哪个TextView去显示字符串啊。
/**
* Constructor
*
* @param context The current context.
* @param resource The resource ID for a layout file containing a layout to use when
* instantiating views.
* @param textViewResourceId The id of the TextView within the layout resource to be populated
* @param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, int textViewResourceId, List<T> objects) {
init(context, resource, textViewResourceId, objects);
}
其中resource还是布局文件,不过是我们自定义的layout文件了,而textViewResourceId就是我们定义的textView了,下面是Activity中的代码:
ListView listView = (ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.arrayadater, R.id.textView1, list);
listView.setAdapter(adapter);
只是利用一个新的构造函数重新创建了一个ArrayAdapter对象,下面是效果图:
在上面,我们只是定义了一个TextView,那么,我们是不是可以定义多一个其它的component呢,比如再定义一个Checkbox呢,我们来试试吧。
<?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" >
<CheckBox android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView android:id="@+id/textView1"
android:drawableLeft="@drawable/ic_launcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
接下来看看效果图:
我们可以看到Checkbox出来了,那我们现在随便选中几个吧,然后继续往下拉,我们可以看到,下面的item,还是被选中了,为什么呢?这正好说明了,这些view的确是在一直被重新利用的。
大家有兴趣,可以接下去看关于SimpleAdapter的使用:
或者再看一下上一篇文章:
分享到:
相关推荐
android中ArrayAdapter SimpleAdapter BaseAdapter 的使用
该例中包含了常用的Adapter的使用,如baseadapter、simpleAdapter、SimlpeCursorAdapter和ArrayAdapter,例子写在一个工程中的。请注意切换清单文件中启动的Activity。
android-custom-arrayadapter, 一个简单的示例应用程序,演示如何使用定制的ArrayAdapter Android中的定制ArrayAdapter示例这个应用程序演示如何创建和使用自定义ArrayAdapter来显示自定义列表项视图,该视图比标准...
主要介绍了简单好用的Adapter---ArrayAdapter详解,具有一定参考价值,需要的朋友可以了解下。
主要介绍了Android自定义Spinner下拉列表(使用ArrayAdapter和自定义Adapter实现)的相关资料,需要的朋友可以参考下
ListView是Android中经常会使用的东西,绑定数据对于初学者来说,尤其是刚接触编程的人来说,往往会觉得很难理解,我上大二的时候学的java,但是基本上相当于没有学,什么都没写过,真正接触编程就是开始上手学...
RecyclerView doesn't ship a ready to go RecyclerView.Adapter implementation as ListView did with the ArrayAdapter. This library is this missing ArrayAdapter. Easy to use Familiar API known from ...
android-custom-array-adapter-demo, Android演示如何使用定制适配器填充 ListView Android定制适配器演示这里示例演示如何使用自定义布局模板显示一个基于 array 数据源的ListView 。 在这种情况下,有一个 User ...
ArrayAdapter:支持泛型操作,最简单的一个Adapter,只能展现一行文字 SimpleAdapter:同样具有良好扩展性的一个Adapter,可以自定义多种效果 SimpleCursorAdapter:用于显示简单文本类型的listView,一般在数据库...
android中的Adapter使用,比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。
andrdoid arrrayadapter测试
ArrayAdapter和simpleAdapter的回顾,好长时间都没有写android了,今天出来溜溜,这个写的是最初的功能。 就是listview。simpleAdapter是左图片右文本、 这个适合初学者玩下。
由图可以看到在Android中与Adapter有关的所有接口、类的完整层级图。在我们使用过程中可以根据自己的需求实现接口或者继承类进行一定的扩展。比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,...
三个示例代码,分别演示了ArrayAdapter,SimpleAdapter,和BaseAdapter的用法。
ToDoList, 使用定制的ArrayAdapter, Android Studio 0.5.1开发.
ArrayAdapter<CharSequence> aAdapter = ArrayAdapter.createFromResource(this, R.array.province, android.R.layout.simple_spinner_item); aAdapter.setDropDownViewResource(android.R.layout.simple_spinner_...
android 适配器用法总结、比较常用的有 BaseAdapter,SimpleAdapter,ArrayAdapter,SimpleCursorAdapter等。