Android应用开发学习之列表视图
作者:刘昊昱
博客:http://blog.csdn.net/liuhaoyutz
列表视图我们会经常用到,可以通过两种方式来创建列表视图,一种方式是直接使用ListView组件创建,另一种方式是通过让Activity继承ListActivity实现。
指定ListView组件的内容有两种方法,一是通过在布局文件中指定数组资源,另一种是通过创建适配器。
下面来看一个使用ListView组件创建列表视图的例子,其内容是通过在布局文件中指定数组资源实现的。该程序运行效果如下:
主布局文件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" > <ListView
android:id="@+id/listView"
android:entries="@array/brands"
android:layout_height="wrap_content"
android:layout_width="match_parent"/> </LinearLayout>
数组资源定义在BrandArray.xml文件中,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="brands">
<item>苹果</item>
<item>三星</item>
<item>HTC</item>
<item>诺基亚</item>
<item>联想</item>
<item>华为</item>
<item>魅族</item>
<item>索尼</item>
<item>夏新</item>
<item>小米</item>
<item>天语</item>
<item>中兴</item>
<item>其它</item>
</string-array>
</resources>
主Activity文件内容如下:
package com.liuhaoyu; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); final ListView listView=(ListView)findViewById(R.id.listView);
listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String result = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
} });
}
}
上面的例子中,ListView组件的内容是通过数组资源指定的,下面看一个通过创建适配器指定ListView组件内容的例子,该程序运行效果如下图所示:
该程序主布局文件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" > <ListView
android:id="@+id/listView"
android:layout_height="wrap_content"
android:layout_width="match_parent"/> </LinearLayout>
主Activity文件内容如下:
package com.liuhaoyu; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); // 通过在代码中定义数组来创建适配器.
String[] brand=new String[]{"苹果","三星","HTC","诺基亚","联想","华为","魅族","索尼","夏新","小米","天语","中兴","其它"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,brand); // 也可以使用数组资源来创建适配器,而不是象上面那样在代码中定义 数组。
// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
// this, R.array.brands,android.R.layout.simple_list_item_1); final ListView listView=(ListView)findViewById(R.id.listView);
listView.setAdapter(adapter); listView.setOnItemClickListener(new OnItemClickListener() { @Override
public void onItemClick(AdapterView<?> parent, View arg1, int pos,
long id) {
String result = parent.getItemAtPosition(pos).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
} });
}
}
以上是通过ListView组件创建的列表视图,我们前面说过,还有一种创建列表视图的方法是通过让Activity继承ListActivity实现。下面我们来看一个例子,该程序运行效果如下图所示:
该程序的主Activity文件内容如下:
package com.liuhaoyu; import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast; public class MainActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); // 通过在代码中定义数组来创建适配器.
String[] brand=new String[]{"苹果","三星","HTC","诺基亚","联想","华为","魅族","索尼","夏新","小米","天语","中兴","其它"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,brand); // 也可以使用数组资源来创建适配器,而不是象上面那样在代码中定义 数组。
// ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
// this, R.array.brands,android.R.layout.simple_list_item_1); setListAdapter(adapter);
} @Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String result = l.getItemAtPosition(position).toString();
Toast.makeText(MainActivity.this, result, Toast.LENGTH_SHORT).show();
} }
在这个程序中,MainActivity继承了ListActivity,所以本身就具有列表视图的属性。需要注意的一点是,不能像以前的程序那样调用setContentView()方法,如果调用了这个方法,运行时会出错。
最后,我们来看一下怎样创建带图标的列表视图。下图是该程序的运行效果:
这个程序需要用到SimpleAdapter类,下面是Android官方网站上到该类的一个介绍:
An easy adapter to map static data to views defined in an XML file. You can specify the data backing the list as an ArrayList of Maps. Each entry in the ArrayList corresponds to one row in the list. The Maps contain the data for each row. You also specify an XML file that defines the views used to display the row, and a mapping from keys in the Map to specific views.
来看一下SimpleAdapter类的构造函数:
public SimpleAdapter (Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from,
int[] to)
|
context |
The context where the View associated with this SimpleAdapter is running |
|
data |
A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from" |
|
resource |
Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to" |
|
from |
A list of column names that will be added to the Map associated with each item. |
|
to |
The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter |
结合以上定义,现在来看一下我们的示例程序的主Activity文件的实现,其内容如下:
package com.liuhaoyu; import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter; public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); ListView listview = (ListView) findViewById(R.id.listView); int[] imageId = new int[] { R.drawable.image01, R.drawable.image02,
R.drawable.image03, R.drawable.image04, R.drawable.image05,
R.drawable.image06, R.drawable.image07, R.drawable.image08,
R.drawable.image09, R.drawable.image10 };
String[] title = new String[] { "时钟", "E-mail", "音乐", "电话", "日历",
"游戏", "YouTube", "地图", "Google+", "通讯录" };
List<Map<String, Object>> listItems = new ArrayList<Map<String, Object>>(); for (int i = 0; i < imageId.length; i++) {
Map<String, Object> map = new HashMap<String, Object>(); // 实例化Map对象
map.put("image", imageId[i]);
map.put("title", title[i]);
listItems.add(map);
} SimpleAdapter adapter = new SimpleAdapter(this,
listItems,
R.layout.items,
new String[] { "title", "image" },
new int[] {R.id.title, R.id.image });
listview.setAdapter(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" > <ListView
android:id="@+id/listView"
android:layout_height="wrap_content"
android:layout_width="match_parent"/> </LinearLayout>
另外,根据SimpleAdapter类的要求,我们需要创建一个描述每一行布局的xml文件,这里命名为items.xml,其内容如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/image"
android:paddingRight="10px"
android:paddingTop="20px"
android:paddingBottom="20px"
android:adjustViewBounds="true"
android:maxWidth="72px"
android:maxHeight="72px"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10px"
android:layout_gravity="center"
android:id="@+id/title"
/>
</LinearLayout>
Android应用开发学习之列表视图的更多相关文章
- Android应用开发学习之表格视图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 本文我们来学习一个使用表格视图的程序,下图是该程序的运行效果: 该程序主Activity文件内容如下: packag ...
- Android应用开发学习之画廊视图
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 画廊视图Gallery能按水平方向显示一组图片,并可以拖动图片.下面我们来看一个使用画廊视图的例子,其运行效果如下: ...
- 2021年正确的Android逆向开发学习之路
2021年正确的Android逆向开发学习之路 说明 文章首发于HURUWO的博客小站,本平台做同步备份发布.如有浏览或访问异常或者相关疑问可前往原博客下评论浏览. 原文链接 2021年正确的Andr ...
- 【Android】9.3 自定义列表视图的外观
分类:C#.Android.VS2015: 创建日期:2016-02-18 一.简介 自定义的列表视图通常用Resources/Layout文件夹下的axml文件中的资源来声明,适配器则通过Id去加载 ...
- Android之怎样使用ListView列表视图
ListView 列表视图创建方法: (1)直接使用ListView 组件创建 (2)让Activity继承ListActivity实现 第一种:在XML中直接使用ListView 组件创建 在val ...
- Android使用Mono c#分段列表视图
下载source code - 21.7 KB 你想知道如何把多个ListView控件放到一个布局中,但是让它们在显示时表现正确吗 多个列表项?你对它们正确滚动有问题吗?这个例子将向你展示如何组合单独 ...
- Android 系统开发学习杂记(转)
http://blog.csdn.net/shagoo/article/details/6709430 > 开发环境1.安装 Eclipse 和 android-sdk 并解压安装2.Eclip ...
- Android应用开发学习之相对布局
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz 相对布局RelativeLayout是指按照组件之间的相对位置进行布局,如一个组件在另一个组件的左边.右边.上边或下 ...
- Android应用开发学习笔记之播放音频
作者:刘昊昱 博客:http://blog.csdn.net/liuhaoyutz Android支持常用音视频格式文件的播放,本文我们来学习怎样开发Android应用程序对音视频进行操作. Andr ...
随机推荐
- Expat Parser解析xml文件
Expat 解析器是基于事件的解析器. 基于事件的解析器集中在 XML 文档的内容,而不是它们的结构.正因为如此,基于事件的解析器能够比基于树的解析器更快地访问数据. 请看下面的 XML 片段: &l ...
- PERL高效代码摘录 - 数组
1. 2个数组找不同 ,,,,,,); ,); %seen=(); foreach(@a_hast_g){ ; } @unique=grep($seen{$_},@a_cl_g); } map { $ ...
- 【javascript 变量和作用域】
今天学习了javascript 的变量和作用域的基本知识,对于以前在开发中遇到的一些不懂的小问题也有了系统的认识,收获还是比较多的. [基本类型和引用类型] ECMAScript 变量可能包含两种不同 ...
- jsp调用javabean出现错误HTTP Status 500 - Unable to compile class for JSP
HTTP Status 500 - Unable to compile class for JSP: type Exception report message Unable to compile ...
- Castle ActiveRecord配置文件中连接字符串解密
使用Castle ActiveRecord通常都是使用配置文件进行数据库连接配置.然后采用如下方式初始化: IConfigurationSource source = ConfigurationMan ...
- 【bzoj】1026: [SCOI2009]windy数
1026: [SCOI2009]windy数 Description windy定义了一种windy数.不含前导零且相邻两个数字之差至少为2的正整数被称为windy数. windy想知道,在A和B之间 ...
- CCTV评论员评论步行者与奇才的比赛
步行者客场迎战主场作战的奇才,奇才的战士可能由于过度兴奋或是过度紧张身体僵硬,本来能打进的球都失掉了.反而,由于步行者取得了两位数的领先,越大心情越放松,打出了过去很少见的流畅局面. CCTV评论员就 ...
- 在 IIS MIME 类型中添加 md 扩展名
最近在了解 Knowledge Base (知识库)的内容,对两个平台比较感兴趣,一个是 Raneto,一个是 MDwiki,两者都是使用md文件作为内容存储. 需要注意的是,使用IIS部署网站后,需 ...
- 欧几里得旅行商问题 java与c++实现
双调欧几里得旅行商问题是一个经典动态规划问题.<算法导论(第二版)>思考题15-1 旅行商问题描述:平面上n个点,确定一条连接各点的最短闭合旅程.这个解的一般形式为NP的(在多项式时间内可 ...
- Waterfall———瀑布流布局插件, 类似于 Pinterest、花瓣、发现啦。
瀑布流布局插件, 类似于 Pinterest.花瓣.发现啦. En 中文 文档 下载 下载waterfall插件最新版本. 使用 html: <div id="container&qu ...