转载请注明:http://www.cnblogs.com/igoslly/p/6947225.html

下一章是关于ListFragment的内容,首先先介绍ListView的相关配置,理解ListFragment也相较容易。

在fznpcy专栏:http://blog.csdn.net/fznpcy/article/details/8658155/有较为详尽的讲解和范例。

ListView & Adapter

一、Adapter

Adapter是连接后端数据和前端显示的适配器接口,是数据和UI(View)之间一个重要的纽带。

在常见的View(List View,Grid View)等地方都需要用到Adapter。

如下图直观的表达了Data、Adapter、View三者的关系:

Adapter存在多种形式,但我们这里只介绍常用的ArrayAdapter、SimpleAdapter和BaseAdapter

二、ArrayAdapter

每个Adapter都要自定义各自的item布局文件,ArrayAdapter只包含文字,一般使用android.R.layout.simple_list_item_1(系统定义好的布局文件只显示一行文字)

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:textColor="#fff"
android:background="?android:attr/activatedBackgroundIndicator"
android:minHeight="?android:attr/listPreferredItemHeightSmall"/>

创建ArrayAdapter

String[] strs = {"1","2","3","4","5"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.xxx,strs);

当然也可以在Android Studio —  strings.xml中直接定义ArrayList。

    <string-array name="list_item">
<item>Practice Record</item>
<item>Competition Record</item>
<item>Previous History</item>
<item>Game Rules</item>
<item>How to use this app?</item>
<item>User Information</item>
</string-array>

在App的Drawer Layout中既可以简单使用,设定菜单列表。

三、SimpleAdapter

SimpleAdapter较ArrayAdapter能实现更复杂的结构,可放ImageView(图片),Button(按钮),CheckBox(复选框)等等。

包含ListView可直接继承ListActivity,和普通的Activity没有太大的差别,不同就是对显示ListView做了许多优化,方面显示而已。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="8"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Null"
android:textSize="20sp"
android:padding="2dp"
android:textColor="@color/black"
android:id="@+id/list_competition_player"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="Null"
android:textSize="16sp"
android:padding="2dp"
android:id="@+id/list_competition_date"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="100"
android:gravity="center"
android:textSize="24sp"
android:textColor="@color/black"
android:id="@+id/list_competition_scoreA"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="—"
android:gravity="center"
android:textSize="28sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="3"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="100"
android:gravity="center"
android:textSize="24sp"
android:textColor="@color/black"
android:id="@+id/list_competition_scoreB"/>
</LinearLayout>
</LinearLayout>

接收参数包含

this,布局文件(alist.XML),Hash Map,布局文件控件Id。Hash Map的每个键值数据,和布局文件中对应id的组件,一一映射。

创建列表ArrayList,每个元素即是单独的Hash Map,根据key word关键字添加字符串 / 图片名

map.put("key_word1","value1");
List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
Map<String, Object> map = new HashMap<String, Object>();
map.put("title", "G1");
map.put("info", "google 1");
map.put("img", R.drawable.i1);
list.add(map);

创建SimpleAdapter

SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.vlist,
new String[]{"title","info","img"},
new int[]{R.id.title,R.id.info,R.id.img});

实际效果图

包含图片的效果图

四、BaseAdapter

在列表上添加Button,实时修改Adapter,都必须使用BaseAdapter。

SimpleAdapter也继承于BaseAdapter。

BaseAdapter是抽象类,必须自定义一个Adapter继承它,也可结合ViewHolder进行显示。

/* 当List有大量的数据需要加载的时候,会占据大量内存,影响性能,这时候就需要按需填充并重新使用view来减少对象的创建

* 最快的方式是定义一个Viewholder,将convextag设置为Viewholder,不为空时重新使用即可

* findViewById是在解析layout.xml布局那种其中的子View,解析xml是一个力气活,所以提出了ViewHolder的概念

* 使用一个静态类,保存xml中的各个子View的引用关系,这样就不必要每次都去解析xml了。

*/

继承BaseAdapter定义时,必须重载4个函数:getCount()、getItem(int position)、getItemId(int position)、getView(int position, View convertView, ViewGroup parent)

    public class CompetitionListAdapter extends BaseAdapter {
private LayoutInflater mInflater=null;
public CompetitionListAdapter(Context context){
this.mInflater=LayoutInflater.from(context);
}
@Override
public int getCount(){
return competitionlist.size();
}
@Override
public Object getItem(int position){
return competitionlist.get(position);
}
@Override
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
if (convertView ==null){
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.history_list_competition,null);
holder.date=(TextView)convertView.findViewById(R.id.list_competition_date);
holder.scoreA=(TextView)convertView.findViewById(R.id.list_competition_scoreA);
holder.scoreB=(TextView) convertView.findViewById(R.id.list_competition_scoreB);
holder.player=(TextView)convertView.findViewById(R.id.list_competition_player);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.date.setText((String)competitionlist.get(position).get("date"));
holder.scoreA.setText((String)competitionlist.get(position).get("scoreA"));
holder.scoreB.setText((String)competitionlist.get(position).get("scoreB"));
holder.player.setText((String)competitionlist.get(position).get("player"));
return convertView;
}
private class ViewHolder{
public TextView date;
public TextView player;
public TextView scoreB;
public TextView scoreA;
}
}

当数据源有修改时,需要notifyDataSetChanged()告知。

        public void setDataList(List<Map<String,Object>> list){
if (list!=null){
competitionlist=list;
notifyDataSetChanged();
}
}

本笔记内容均为个人学习整理,转载请注明博客园-igoslly

Android开发笔记(12)——ListView & Adapter的更多相关文章

  1. android学习笔记12——ListView、ListActivity

    ListView.ListActivity ==> ListView以垂直列表的形式显示所有列表项. 创建ListView的方式: 1.直接使用ListView创建 2.Activity继承Li ...

  2. 【转】Android开发笔记(序)写在前面的目录

    原文:http://blog.csdn.net/aqi00/article/details/50012511 知识点分类 一方面写写自己走过的弯路掉进去的坑,避免以后再犯:另一方面希望通过分享自己的经 ...

  3. Android开发笔记——以Volley图片加载、缓存、请求及展示为例理解Volley架构设计

    Volley是由Google开源的.用于Android平台上的网络通信库.Volley通过优化Android的网络请求流程,形成了以Request-RequestQueue-Response为主线的网 ...

  4. [置顶] Android开发笔记(成长轨迹)

    分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API ...

  5. 【转】Android开发笔记——圆角和边框们

    原文地址:http://blog.xianqu.org/2012/04/android-borders-and-radius-corners/ Android开发笔记——圆角和边框们 在做Androi ...

  6. 《ArcGIS Runtime SDK for Android开发笔记》

    开发笔记之基础教程 ArcGIS Runtime SDK for Android 各版本下载地址 <ArcGIS Runtime SDK for Android开发笔记>——(1).And ...

  7. Android开发笔记:打包数据库

    对于数据比较多的控制一般会加入SQLite数据库进行数据存储,在打包时这些数据库是不自动打包到apk中的,如何创建数据库呢 方法1:将创建数据库的sql语句在SQLiteHelper继承类中实现,在第 ...

  8. Android开发笔记--hello world 和目录结构

    原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SD ...

  9. [APP] Android 开发笔记 003-使用Ant Release 打包与keystore加密说明

    接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genk ...

  10. [APP] Android 开发笔记 002-命令行创建默认项目结构说明

    接上节:[APP] Android 开发笔记 001 4. 默认项目结构说明: 这里我使用Sublime Text 进行加载.

随机推荐

  1. NOIP2018 滚粗记

    Day -2  上午,大家都在复习各种模板,zhx总结了足足67个模板(杨辉三角也算模板???),lgl死磕FFT发现cos和sin打反了,我也是复习板子和以前做过的题,几乎没有人颓. 接着jdr,l ...

  2. openoffice启动服务并将office文件转换为pdf文件

    1.首先下载最新版的openoffice工具,安装完成之后安装服务,, win+r打开命令提示符 输入cmd,cd C:\Program Files (x86)\OpenOffice 4\progra ...

  3. ubuntu_linux /boot/grub/grub.conf

    ==========================================UBUNTU  /boot/grub/grub.conf文件============================ ...

  4. 一个电商项目的Web服务化改造

    一个电商项目的Web服务化改造 项目,早期是随便瞎做的,没啥架构,连基本的设计也没. 有需求,实现需求,再反复修改. 大致就是这么做的. 最近,项目要重新架构,和某boss协商的结果是,采用阿里开源的 ...

  5. bupt summer training for 16 #4 ——数论

    https://vjudge.net/contest/173277#overview A.平方差公式后变为 n = (x + y)(x - y) 令 t = x - y ,变成 n = (t + 2x ...

  6. [HDU1403]Longest Common Substring(后缀数组)

    传送门 求两个串的公共子串(注意,这个公共子串是连续的一段) 把两个串连在一起,中间再加上一个原字符串中不存在的字符,避免过度匹配. 求一遍height,再从height中找满足条件的最大值即可. 为 ...

  7. Pie POJ 3122 二分搜索

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 17324   Accepted: 5835   Special Judge ...

  8. [bzoj3192][JLOI2013]删除物品_树状数组_栈

    删除物品 bzoj-3192 JLOI-2013 题目大意:给你n个物品,分成2堆.所有的物品有不同的优先级.我只可以将一堆中的堆顶移动到另一个堆的堆顶.而如果当前物品是全局所有物品中优先级最高的,我 ...

  9. Spring MVC-处理程序映射(Handler Mapping)-简单的Url处理程序映射(Simple Url Handler Mapping)示例(转载实践)

    以下内容翻译自:https://www.tutorialspoint.com/springmvc/springmvc_simpleurlhandlermapping.htm 说明:示例基于Spring ...

  10. iOS:制作左右侧滑(抽屉式)菜单

    感谢控件作者:https://github.com/SocialObjects-Software/AMSlideMenu 首先上效果图: 这里我们使用AMSlideMenu来实现左右侧滑菜单的效果.控 ...