ListView之ArrayAdapter的使用
来源:互联网 发布:作品集网站 知乎 编辑:程序博客网 时间:2024/05/01 02:06
Android杂谈--ListView之ArrayAdapter的使用
ArrayAdapter
总是感觉写自己的博客才更能够学到东西,网上尽管有很多好的资料,但是参差不齐,需要浪费大量时间才能够找到最需要的,索性写自己最需要的东西。
Adapter是适配器的意思,在Android中大量的使用到了ListView,而ListView需要与各种适配器结合才能够使用。不同的场合使用不同的适配器,所以总结一下最常用的会对自己以后很有帮助
ArrayAdapter(数组适配器)一般用于显示一行文本信息,所以比较容易。
public ArrayAdapter(Context context,int textViewResourceId,List<T> objects)
上面的这行代码来装配数据,要装配这些数据就需要一个连接ListView视图对象和数组数据的适配器来两者的适配工作,ArrayAdapter的构造需要三个参数,依次为this,布局文件(注意这里的布局文件描述的是列表的每一行的布局,可以参见main.xml文件,android.R.layout.simple_list_item_1是系统定义好的布局文件只显示一行文字,数据源(一个List集合)。同时用setAdapter()将ListView和Adapter绑定。
例子一:可以用来简单的显示一条文字
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="cnblogs--花郎" /> <ListView android:id="@+id/listview" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
Activity
package com.loulijun.demo14;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class Demo13Activity extends Activity { private ListView lv; private ArrayList<String> list = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView)findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, android.R.layout.simple_expandable_list_item_1, getData()); lv.setAdapter(adapter); } private ArrayList<String> getData() { list.add("180平米的房子"); list.add("一个勤劳漂亮的老婆"); list.add("一辆宝马"); list.add("一个强壮且永不生病的身体"); list.add("一个喜欢的事业"); return list; }}
注意:这里的android.R.layout.simple_expandable_list_item_1是系统内置布局,样式就是如下,这部分也可以自己定义布局然后倒入进去,但是只能是一个TextView
代码很少,一看就懂。可能麻烦一些的是泛型这部分,不懂的可以翻翻JAVA的基础。运行效果如下
例子二:这个例子可以增加一个ImageView,但是在设置ArrayAdapter的时候需要增加这个自定义的布局
public ArrayAdapter(Context context,int resource,int textViewResourceId,List<T> objects)
上面的第一个参数是上下文,一般为this。第二个参数是自定义的布局文件,比如下面的就是R.layout.list_item。第三个参数是TextView的id,第四个参数是数据
list_item.xml(main.xml与上面一样)
<?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:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_launcher" /> <TextView android:id="@+id/tv" android:layout_width="fill_parent" android:layout_height="wrap_content" /></LinearLayout>
Activity
package com.loulijun.demo14;import java.util.ArrayList;import android.app.Activity;import android.os.Bundle;import android.widget.ArrayAdapter;import android.widget.ListView;public class Demo13Activity extends Activity { private ListView lv; private ArrayList<String> list = new ArrayList<String>(); @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); lv = (ListView)findViewById(R.id.listview); ArrayAdapter<String> adapter = new ArrayAdapter<String>( this, R.layout.list_item, R.id.tv, getData()); lv.setAdapter(adapter); } private ArrayList<String> getData() { list.add("180平米的房子"); list.add("一个勤劳漂亮的老婆"); list.add("一辆宝马"); list.add("一个强壮且永不生病的身体"); list.add("一个喜欢的事业"); return list; }}
- ListView之ArrayAdapter的使用
- ListView之ArrayAdapter的使用
- ListView的Adapter使用 之 初学ArrayAdapter
- Android中ListView之ArrayAdapter的使用
- ListView的使用-ArrayAdapter
- ListView的Adapter使用 之 初学ArrayAdapter<String>
- ListView的Adapter使用 之 初学ArrayAdapter<String>
- ListView的Adapter使用 之 初学ArrayAdapter<String>
- ListView的Adapter使用 之 初学ArrayAdapter<String>
- Android实例demo11之listview的简单使用(arrayadapter、simpleadapter)
- Android学习之ListView与ArrayAdapter的简单使用
- ListView的Adapter使用 之 初学ArrayAdapter<String>
- ListView的Adapter使用 之 初学ArrayAdapter<String>
- android ListView之——ArrayAdapter使用
- android组件ListView之ArrayAdapter简单使用
- ListView的使用----ArrayAdapter与SimpleAdapter
- Android ListView ArrayAdapter 的简单使用
- ListView之ArrayAdapter详解
- json解析原理
- 九度 OJ 1054:字符串内排序
- 2016-05-25 margin-right jsp获取页面流变量 文字颜色
- 合并排序
- 多线程编程艺术(3)-多线程等待通知编程模型
- ListView之ArrayAdapter的使用
- cordys CWS 编译失败 需要指定 JRE
- JAVA 笔记(三) 从源码深入浅出集合框架
- LinkedHashMap源码解析
- Android之TextView效果
- View滑动的原理,解析scrollTo,ScrollBy和Scroller
- 封装一个类处理安卓客户端与服务器端交互
- 轻量web服务器开发日记02-mempool内存池的实现
- UISearchBar 使用技巧