展会信息港展会大全

android:listView学习记录1
来源:互联网   发布日期:2015-11-26 14:52:35   浏览:1743次  

导读:listView垂直列表显示组件,创建的方式有两种:第一种:直接使用ListView进行创建;第二种:让Activity继承ListActivity创建。与listView其他相关的基本...

listView垂直列表显示组件,创建的方式有两种:

第一种:直接使用ListView进行创建;

第二种:让Activity继承ListActivity创建。

与listView其他相关的基本元素有:数据集、适配器。它们三者关系是典型的MVC模型应用。

几种重要的属性:

android:entries:制定一个数组资源,android将根据这个数组资源生成listView;

android:divider:设置List列表项的分隔条(即可用颜色,也可用Drawable分割)

下面给个实例:使用SimpleAdapter创建ListView

布局文件:Main.xml

[html]

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<ListView

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:id="@+id/list"

>

</ListView>

<ImageView

android:id="@+id/imageView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:paddingLeft="10dp"

></ImageView>

<TextView

android:id="@+id/textView"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:gravity="center"

android:textSize="20sp"

></TextView>

</LinearLayout>

MainActivity.java

[java]

package com.javaee;

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.util.Log;

import android.widget.ListView;

import android.widget.SimpleAdapter;

public class MainActivity extends Activity {

/** Called when the activity is first created. */

private String[] names = new String[] { "疯狂编程", "热爱编程", "重复编程", "坚持编程",

"一定能立足" };

private int[] images = new int[] { R.drawable.image1, R.drawable.image2,

R.drawable.image3, R.drawable.image4, R.drawable.image5 };

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

ListView listView = (ListView) findViewById(R.id.list);

List<Map<String, Object>> maps = new ArrayList<Map<String, Object>>();

for (int i = 0; i < names.length; i++) {

Map<String, Object> map = new HashMap<String, Object>();

map.put("name", names[i]);

map.put("image", images[i]);

maps.add(map);

}

SimpleAdapter adapter = new SimpleAdapter(this, maps, R.layout.main,

new String[] { "name", "image" }, new int[] { R.id.textView,

R.id.imageView });

listView.setAdapter(adapter);

}

}

SimpleAdapter的几个参数进行解释下:

第一个参数context:上下文环境;

第二个参数data:传入一个List<Map<String,?>>类型的集合对象,该集合对象中每个Map对象生成一个列表项;

第三个参数resource:指定一个界面布局的ID;

第四个参数from:传入一个String[]类型的参数,该数组里面的数据项对应的是Map里面的KEY,通过KEY再取出Value;www.2cto.com

第五个参数to:传入一个int[]类型的参数,该参数决定使用哪些View的组件来显示从Map中取出的Value,并组合成一个列表项。

过程中出现的错误:

java.lang.UnsupportedOperationException: addView(View, LayoutParams) is not supported in AdapterView

这个错误原因是我在main.xml布局文件中,将<ImageView>、<TextView>控件包含在了<ListView>控件内部而出现的错误,在设计中用来显示数据的控件,最好放在另外一个布局文件中。

赞助本站

人工智能实验室

相关热词: android开发 教程

相关内容
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港