Android学习第三天————ListView组件

news/2024/7/3 11:07:18

ListView是将内容以列表的形式显示出来,它能够自适应内容的长度

一、下面是通过XML文件创建一个ListView

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView 
        android:id="@+id/listview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/mylist"引用外部的配置文件
        ></ListView>
    <!-- android:entries="@array/mylist" -->
</LinearLayout>
在values文件夹下新建一个xml文件里面放置前面代码引用内容

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="mylist">设置文件的名称
        <item>第一行</item>
        <item>第二行</item>
        <item>第三行</item>
        <item>第四行</item>
    </string-array>
</resources>
通过以上设置即可在开始页面中以列表的形式显示第一行....第四行的文本信息
二、java代码实现

(1)、通过适配器(ArrayAdapter该适配器只能显示文本信息)来设置ListView的值

1、首先要获得ListView对象

2、然后创建一个字符串数组用来存储需要显示的信息

3、创建一个适配器

4、将适配器放到listview中

//获取ListView对象
    	ListView listView=(ListView)findViewById(R.id.listview1);
    	//设置一个数组
    	String[] strings={"哈哈","呵呵","嘿嘿"};
    	ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,android.R.layout.simple_expandable_list_item_1,strings);//<span style="font-family: Arial, Helvetica, sans-serif;">simple_expandable_list_item_1是以单行的形式显示内容</span>
    	listView.setAdapter(adapter);
(2)通过SimpleAdapter适配器来设置ListView的值

1、首先创建一个XML文件,在其中放置一个ListView

2、创建一个XML文件,在其中放置需要显示在ListView中显示的组件

3、在java代码中获取ListView对象

4、创建SimpleAdapter适配器对象SimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to)

参数context:上下文,比如this。关联SimpleAdapter运行的视图上下文

参数data:Map列表,列表要显示的数据,这部分需要自己实现,如例子中的getData(),类型要与上面的一致,每条项目要与from中指定条目一致

参数resource:ListView单项布局文件的Id,这个布局就是你自定义的布局了,你想显示什么样子的布局都在这个布局中。这个布局中必须包括了to中定义的控件id

参数 from:一个被添加到Map上关联每一个项目列名称的列表,数组里面是列名称

参数 to:是一个int数组,数组里面的id是自定义布局中各个控件的id,需要与上面的from对应

5、将适配器放到ListView中

示例代码

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ListView 
        android:id="@+id/listview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ></ListView>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <ImageView 
        android:id="@+id/imglist"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />
	<TextView 
	    android:id="@+id/textlist"
	    android:layout_width="wrap_content"
        android:layout_height="wrap_content"
	    />
</LinearLayout>
//获取ListView对象
    	ListView listView=(ListView)findViewById(R.id.listview1);
    	List<Map<String , Object>> list=new ArrayList<Map<String,Object>>();
    	Map<String, Object> map=new HashMap<String, Object>();
    	map.put("img", R.drawable.tu1);
    	map.put("text", "图片");
    	list.add(map);
    	map=new HashMap<String, Object>();
    	map.put("img", R.drawable.tu2);
    	map.put("text", "图片");
    	list.add(map);
    	SimpleAdapter simpleAdapter=new SimpleAdapter(this, list,
    			R.layout.list_content_activity, new String[]{"img","text"},new int[]{R.id.imglist,R.id.textlist});
    	listView.setAdapter(simpleAdapter);
三、ListView的事件

LIstView中的单击事件,选中其中的某一个项目来触发相应的事件

示例代码

listView.setOnItemClickListener(new OnItemClickListener() {
    		//position在列表项中的位置(下标从0开始)
    		//id在适配器中的位置
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Toast.makeText(MainActivity.this, "第"+position+"项被点击", Toast.LENGTH_SHORT).show();
				
			}
		});









http://www.niftyadmin.cn/n/3654901.html

相关文章

Android中实现横屏竖屏的切换

<span style"font-size:18px;">Android中横屏竖屏的切换</span> <span style"font-size:18px;"></span> <span style"font-size:18px;"></span> <span style"font-size:18px;">Configurati…

安全的线程同步

安全的线程同步Jeffrey Richter 著刘未鹏 译[msdn.2003.01]到目前为止&#xff0c;线程同步最为普遍的用途是确保多线程对共享资源的互斥访问。对于同步单个进程中的多个线程以实现互斥访问&#xff0c;Win32 API中的CRITICAL_SECTION结构以及与它相关的函数提供了最为快速和高…

Android学习第四天————ListView用BaseAdapter适配器来填充数据

一、BastAdapter适配器获得数据&#xff0c;用来填充ListView组件 1、通过XML文件来创建ListView组件 示例代码 <RelativeLayout xmlns:android"http://schemas.android.com/apk/res/android"xmlns:tools"http://schemas.android.com/tools"android:l…

Who is const ?!铪铪铪铪铪铪

这篇文章主要是看了王咏刚先生的一篇“关于C模板和重载的小问题”而发的&#xff0c;可以说是一篇回贴&#xff0c;之所以独立出来是因为这个问题“有趣”:)先引用一下王咏刚先生的例子&#xff1a;class Node{public: int m; Node(int value) : m(value) {} friend ostream&am…

开始用Google Code的Issue

曾经写了“开源&#xff0c;选择Google Code还是Sourceforge&#xff1f;”&#xff0c;Google Code vs. Sourceforge&#xff0c;这不过是一年&#xff08;未到&#xff09;前的事情&#xff0c;如今Google Code又今非昔比了。打算抽时间好好写一篇更新版的&#xff0c;以免误…

Android学习第四天————GridView组件

GridView类似于ListView&#xff0c;不过它可以一列显示多个组件 一、GridView的常用属性 <GridView android:id"id/gridview"android:layout_width"wrap_content"android:layout_height"wrap_content"android:verticalSpacing"10dp&qu…

开源,选择Google Code还是Sourceforge?(修订版)

本文出处&#xff1a;http://www.winxgui.cn/blog/?p172 &#xff08;版权声明&#xff09;一年前&#xff0c;我写下了“开源&#xff0c;选择Google Code还是Sourceforge&#xff1f;”&#xff0c;如今&#xff0c;Google Code又今非昔比了。抽时间好好整理了下&#xff0c…

Android学习第四天————AutoCompleteTextView自填充文本框

AutoCompleteTextView自填充文本提示框 AutoCompleteTextView是一个需要通过适配器来填充数据的组件&#xff0c;它有两种方式来添加数据(一种是是同XML来添加&#xff0c;另一种是通过适配器来填充数据) 1、适配器来添加数据 通过ArrayAdapter来绑定数据其中第二个参数是数…