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个 ...
随机推荐
- POJ1062 昂贵的聘礼(带限制的spfa)
Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...
- OkHttp3的简单使用(二)
OkHttp3的简单封装 public class OkHttpUtil { public static final String TAG="OkHttpUtil"; privat ...
- C# 的 Task、Thread、ThreadPool 之间有什么异同?
Thread就是Thread,需要自己调度,适合长跑型的操作. ThreadPool是Thread基础上的一个线程池,目的是减少频繁创建线程的开销.线程很贵,要开新的stack,要增加CPU上下文切换 ...
- [web] [vscode] 自定义语言缩进
vscode 默认的html 语言的缩进有点过,貌似一个tab6个space, html看起来太空了,所幸的是可以自己调整单个语言的缩进模式. 方法如下 Preferences: Open User ...
- web集群时session同步的3种方法
在做了web集群后,你肯定会首先考虑session同步问题,因为通过负载均衡后,同一个IP访问同一个页面会被分配到不同的服务器上,如果session不同步的话,一个登录用户,一会是登录状态,一会又不是 ...
- layui之弹出层--从父窗口传递数据到子窗口
原文链接:https://blog.csdn.net/Code_shadow/article/details/80524633 var Index = layer.open({ title: &quo ...
- sqlite数据库文件查看
- renturn 错误
package ui.cfg; import java.awt.BorderLayout; import java.awt.FlowLayout; import javax.swing.JButton ...
- 洛谷P4494 [HAOI2018]反色游戏(tarjan)
题面 传送门 题解 我们先来考虑一个联通块,这些关系显然可以写成一个异或方程组的形式,形如\(\oplus_{e\in edge_u}x_e=col_u\) 如果这个联通块的黑色点个数为奇数,那么显然 ...
- Python之路番外:PYTHON基本数据类型和小知识点
Python之路番外:PYTHON基本数据类型和小知识点 一.基础小知识点 1.如果一行代码过长,可以用续行符 \换行书写 例子 if (signal == "red") and ...