展会信息港展会大全

android中ListView的简单使用
来源:互联网   发布日期:2015-11-26 14:14:16   浏览:1205次  

导读:ListView控件在android中控件类中,算是比较复杂的一种,其实现一般包含以下几个部分1)数据的准备2)构建适配器3)显示数据在使用ListVIew这个控件时,我发现好多人都出...

ListView控件在android中控件类中,算是比较复杂的一种,其实现一般包含以下几个部分

1)数据的准备

2)构建适配器

3)显示数据

在使用ListVIew这个控件时,我发现好多人都出现了这样的问题"your content must have a listview whose id attribute is"android.R.id.List”错误,

然后好多网友给出的答案就是,在.xml中 android:id="@+id/list" 改为 android:id="@+id/android:list“

其实并不一定是这样的错误。首先要区别采用的是何种使用ListView方法。ListView的控件有两种使用方法,

一、使用android内部的ListView【以一个例子来说明】

1.主.xml文件 【注意红色部分】

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

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity"

android:orientation="vertical">

<ListView

android:id="@+id/android:list"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:drawSelectorOnTop="false"

android:scrollbars="vertical"

></ListView>

</LinearLayout>

2.ListView显示的内容.xml

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

android:id="@+id/linelayout"

android:layout_height="wrap_content"

android:layout_width="fill_parent"

android:orientation="horizontal">

<TextView

android:id="@+id/key"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

android:textColor="#aa0000"

/>

<TextView

android:id="@+id/value"

android:layout_height="wrap_content"

android:layout_width="wrap_content"

/>

</LinearLayout>

3.java文件

继承了ListActivity类,而不是Activity类

public class MainActivity extends ListActivity {

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

//注意此处没有用findViewById来取得ListView控件的id

//创建hashmap型数据

ArrayList<HashMap<String,String>> mylist=new ArrayList<HashMap<String,String>>();

HashMap<String,String> map1=new HashMap<String,String>();

HashMap<String,String> map2=new HashMap<String,String>();

map1.put("key", "张三");

map1.put("value", "学生");

map2.put("key", "李四");

map2.put("value", "老师");

mylist.add(map1);

mylist.add(map2);

//构建适配器

SimpleAdapter listAdapter=new SimpleAdapter(this,mylist,R.layout.hashmap,

new String[]{"key","value"},new int[]{R.id.key,R.id.value});

//适配器中的数据时如何传递到ListView的,

setListAdapter(listAdapter);

}

二、自定义ListView

1.主xml文件

与上面只有一处区别

android:id="@+id/list"

2.listview的布局文件: 同上

3.java文件

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

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

ArrayList<HashMap<String,String>> mylist=new ArrayList<HashMap<String,String>>();

HashMap<String,String> map1=new HashMap<String,String>();

HashMap<String,String> map2=new HashMap<String,String>();

map1.put("key", "张三");

map1.put("value", "学生");

map2.put("key", "李四");

map2.put("value", "老师");

mylist.add(map1);

mylist.add(map2);

SimpleAdapter listAdapter=new SimpleAdapter(this,mylist,R.layout.hashmap,

new String[]{"key","value"},new int[]{R.id.key,R.id.value});

//listview这个对象添加setAdapter对象

listview.setAdapter(listAdapter);

赞助本站

人工智能实验室

相关热词: android开发 教程

AiLab云推荐
展开

热门栏目HotCates

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