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

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();
				
			}
		});








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值