ListView与ArrayAdapter的搭配使用
在android中,ListView是一种很重要的控件,一般的使用中,常建立一个所需类型的ArrayList,再通过ArrayAdapter把ListView绑定到ArrayList上,通过ArrayAdapter来使ListView显示和刷新内容。
假定现在有一String类型的ArrayList,叫myArrayList,建立ArrayAdapter并将其与myArrayList绑定的代码如下:
|
1
2
|
ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(this, android.layout.simple_list_item_1, myArrayList); |
其中android.layout.simple_list_item_1是android本身的一个基本listview,在实际中也可以自建一个listview。
当有新的内容时,先将String添加到myArrayList,然后通过以下代码完成ListView的刷新显示:
|
1
2
|
myArrayList.add(0, myString);myArrayAdapter.notifyDataSetChanged(); |
上面add方法的第一个参数是新String要添加的位置,从0开始一次递增。notifyDataSetChanged()的作用是告知ListView刷新内容。
在实际中,经常需要定制ListView,先要为所需的页面、边缘等需要的颜色在colors.xml文件中进行设置。并为页面宽度和页面边缘在dimens.xml中添加所需要的值。
然后需要扩展一个新的TextView类,用作ListView中每一行的显示,在init方法中创建获取前面创立的资源文件,并建立Paint对象,然后重写onDraw方法,利用Paint对象来重写图像。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
import android.content.Context;import android.content.res.Resources;import android.graphics.Canvas;import android.graphics.Paint;import android.util.AttributeSet;import android.widget.TextView; public class WordItemView extends TextView{ private Paint marginPaint; private Paint linePaint; private int paperColor; private float margin; //WordItemView的构造函数 public WordItemView(Context context, AttributeSet ats, int ds){ super(context, ats, ds); init(); } public WordItemView(Context context){ super(context); init(); } public WordItemView(Context context, AttributeSet ats){ super(context, ats); init(); } private void init(){ Resources myResources = getResources(); //创建画刷 marginPaint = new Paint(Paint.ANTI_ALIAS_FLAG); marginPaint.setColor(myResources.getColor(R.color.margin)); linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); linePaint.setColor(myResources.getColor(R.color.lines)); //获得页面背景色和边缘宽度 paperColor = myResources.getColor(R.color.paper); margin = myResources.getDimension(R.dimen.margin); } @Override public void onDraw(Canvas canvas){ //绘制页面颜色 canvas.drawColor(paperColor); //绘制边缘 //canvas.drawLine(0, 0, 0, getMeasuredHeight(), linePaint); canvas.drawLine(0, getMeasuredHeight(), getMeasuredWidth(), getMeasuredHeight(), linePaint); canvas.drawLine(margin, 0, margin, getMeasuredHeight(), marginPaint); //移动文本 canvas.save(); canvas.translate(margin, 0); //渲染文本 super.onDraw(canvas); canvas.restore(); } } |
接下来在res/layout中新建一个xml文件来指定每一个条目在视图列表中的排列方式。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
|
<?xml version="1.0" encoding="utf-8"?> <RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/itemMean" android:layout_height="match_parent" android:layout_width="wrap_content" android:layout_alignParentRight="true" android:textColor="@color/text" android:padding="10dp" android:scrollbars="vertical" android:fadingEdge="vertical"/> <com.qingshuimonk.words.WordItemView android:id="@+id/itemWord" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" android:scrollbars="vertical" android:textColor="@color/text" android:textStyle="italic" android:fadingEdge="vertical"/> </RelativeLayout> |
重写ArrayAdapter方法使其适应现有的空间,在这个例子(一个能显示单词和释义的应用)里,有两个TextView需要显示。
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
|
import java.util.List; import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.ArrayAdapter;import android.widget.LinearLayout;import android.widget.RelativeLayout;import android.widget.TextView; public class MyAdapter extends ArrayAdapter<WordItem>{ int resource; public MyAdapter(Context context, int _resource, List<WordItem> items){ super(context, _resource, items); resource = _resource; } @Override public View getView(int position, View convertView, ViewGroup parent){ LinearLayout newView; WordItem item = getItem(position); String word = item.getWord(); String mean = item.getMean(); if(convertView == null){ newView = new LinearLayout(getContext()); String inflater = Context.LAYOUT_INFLATER_SERVICE; LayoutInflater li; li = (LayoutInflater)getContext().getSystemService(inflater); li.inflate(resource, newView, true); } else{ newView = (LinearLayout)convertView; } TextView wordView = (TextView)newView.findViewById(R.id.itemWord); TextView meanView = (TextView)newView.findViewById(R.id.itemMean); wordView.setText(word); meanView.setText(mean); return newView; } } |
最后在MainActivity里面对ArrayList和ArrayAdapter的绑定代码进行修改。
|
1
2
3
4
|
final ArrayList<WordItem> worditem = new ArrayList<WordItem>(); final MyAdapter adapter = new MyAdapter(this, R.layout.worditem, worditem); wordsList.setAdapter(adapter); |
这样定制的ListView就大功告成了。
ListView与ArrayAdapter的搭配使用的更多相关文章
- 42.Android之ListView中ArrayAdapter简单学习
今天学习下Android中ListView关于ArrayAdapter数据绑定, 废话少说直接上代码. 改下布局文件: <?xml version="1.0" encodin ...
- 第28讲 UI组件之 ListView和ArrayAdapter
第28讲 UI组件之 ListView和ArrayAdapter 1. Adapter 适配器 Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带.在常见的 ...
- Android新手入门2016(8)--ListView之ArrayAdapter
本文来自肥宝传说之路,引用必须注明出处! ListView是Android中经常使用的控件. 什么是列表视图,让我们先看看图: watermark/2/text/aHR0cDovL2Jsb2cuY3N ...
- 深入理解使用ListView时ArrayAdapter、SimpleAdapter、BaseAdapter的原理
在使用ListView的时候,我们传给setAdapter方法的Adapter通常是ArrayAdapter.SimpleAdapter.BaseAdapter,但是这几个Adapter内部究竟是什么 ...
- Android --ListView使用ArrayAdapter
1.继承ArrayAdapter public class TimerDataAdapter extends ArrayAdapter<TimerDataListItem> { //数据I ...
- 怎样在Android中ListView与ArrayAdapter配合使用
[代码]main.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xml ...
- ListView之ArrayAdapter
ArrayAdapter 普通的显示listView子项,安卓的内置对象 使用方法: /* ListView :列表 通常有两个职责: a.将数据填充到布局 b.处理点击事件 一个ListView创建 ...
- AdapterView及其子类之三:基于ListView及ArrayAdapter实现列表
见归档项目ListViewDemo.zip. 基本步骤如下: 1.创建主布局文件,里面包含一个ListView元素. <RelativeLayout xmlns:android="ht ...
- 安卓 listview与arrayadapter
今天有感于群里讨论的一个问题,很简单,但是问题还真是需要仔细看一下 问题:定义了一个最简单的arrayadapter,和listview结合使用,灭个item就显示个最简单的textView,一共6个 ...
随机推荐
- 使用Recyclerview实现图片水平自动循环滚动
简介: 本篇博客主要介绍的是如何使用RecyclerView实现图片水平方向自动循环(跑马灯效果) 效果图: 思路: 1.准备m张图片 1.使用Recyclerview实现,返回无数个(实际Inter ...
- Vue生命周期函数
生命周期函数: 组件挂载,以及组件更新,组建销毁的时候出发的一系列方法. beforeCreate:实例创建之前 created:实例创建完成 beforeMount:模板编译之前 mounted:模 ...
- 下载Chrome浏览器离线安装包
下面提供了window和Mac OS两个版本的Chrome离线版本: Windows版本 Mac OS版本 说明 基本格式是在 chrome 首页的链接 https://www.google.com/ ...
- 以太坊系列之六: p2p模块--以太坊源码学习
p2p模块 p2p模块对外暴露了Server关键结构,帮助上层管理复杂的p2p网路,使其集中于Protocol的实现,只关注于数据的传输. Server使用discover模块,在指定的UDP端口管理 ...
- Docker 的部署方式
在使用 docker run 命令启动 Docker 容器时,如果需要进行端口映射.目录挂载.网络信息等配置,整条命令将变得非常长,并且由于是一条 shell 命令,修改和复用也不方便.我们在大规模部 ...
- 6w4:第六周程序填空题1
描述 下面程序的输出结果是: A::Fun C::Do 请填空: #include <iostream> using namespace std; class A { private: i ...
- 抓包(Charles工具入门)
一.charles工具简单使用 1.录制操作 录制请求.清空录制请求: 两种展示请求的视图方式: 2.录制请求的简单分析 (1)请求的总览页面Overview:可查看请求路径.请求方式.请求时间等有关 ...
- [51nod]1229 序列求和 V2(数学+拉格朗日差值)
题面 传送门 题解 这种颓柿子的题我可能死活做不出来-- 首先\(r=0\)--算了不说了,\(r=1\)就是个裸的自然数幂次和直接爱怎么搞怎么搞了,所以以下都假设\(r>1\) 设 \[s_p ...
- 小白如何将代码上传到github上?
网上已经有很多关于这个的教程,有一步步操作的,但有些感觉已经颇旧了.现在更新一个最新版的github小白教程.尽管以后此教程也会变成旧的,至少在这一段时期,本文还是最新的.就按照github官网上教程 ...
- Python登陆人人网
#!coding:utf-8 import urllib2 import urllib import cookielib def renrenBrower(url,user,password): #登 ...