【Android学习笔记】ListView的使用

1.ListView的第一种使用方式

例子:

activity_main.xml中:(这个LinearLayout是嵌入在外层Layout中,用于显示相当于表格表头标题.ListView用来显示数据)

    <LinearLayout 
        android:layout_width="match_parent"
	    android:layout_height="wrap_content"
	    android:orientation="horizontal"
        >
        <TextView 
			android:layout_width="120dp"
			android:layout_height="30dp"
			android:text="班级"
        />
		<TextView 
			android:layout_width="150dp"
			android:layout_height="30dp"
			android:text="姓名"
			/>
		<TextView 
			android:layout_width="match_parent"
			android:layout_height="30dp"
			android:text="学号"
			/>
    </LinearLayout>
    
    <ListView 
        android:layout_width="match_parent"
    	android:layout_height="match_parent"
       	android:id="@+id/listView"
        />



item.xml中:定义三个没有赋值的TextView

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
    
    <TextView 
        android:layout_width="120dp"
   		android:layout_height="30dp"
        android:id="@+id/clazz"
        />
    <TextView 
        android:layout_width="150dp"
   		android:layout_height="30dp"
        android:id="@+id/name"
        />
    <TextView 
        android:layout_width="match_parent"
   		android:layout_height="30dp"
        android:id="@+id/stuid"
        />

</LinearLayout>


在Activity中给ListView填充信息:

		List<HashMap<String,Object>> data=new ArrayList<HashMap<String,Object>>();
		
		//stulist是一个封装了多个学生信息的List<Student>
		for(Student stu:stulist){
			HashMap<String,Object> item=new HashMap<String,Object>();
			item.put("班级",stu.getClazz());
			item.put("姓名",stu.getName());
			item.put("学号",stu.getStuid());
			data.add(item);
		
		}
		
		//注:这里的context是要显示ListView的Context
		//SimpleAdapter把信息封装在item.xml所定义的格式中,每条信息构成一行
		SimpleAdapter adapter=new SimpleAdapter(context,data,R.layout.item,
			new String[]{"班级","姓名","学号"},new int[]{R.id.clazz,R.id.name,R.id.stuid}
		);
		
		
		listview.setAdapter(adapter);


给ListView添加行的点击监听:

    	list.setOnItemClickListener(new OnItemClickListener() {
    		@Override
    		public void onItemClick(AdapterView<?> parent, View view, int position,
    				long id) {
    			//parent代表当前被点击条目的listView
    			//view代表当前view
    			//position代表当前被绑定的数据在集合中的索引值
    			
    			ListView listView=(ListView) parent;
    			HashMap stuMap= (HashMap) listView.getItemAtPosition(position);
    			
    			Toast.makeText(getApplicationContext(), stuMap.get("姓名").toString(), 1).show();
    			
    		}
    		
	});



2.ListView的第二种使用方式


写一个Activity继承ListActivity,在该activity的xml布局文件中,只定义一个ListView控件,id必须是android.R.id.list@android:id/list

还是定义一个item.xml



--------------------------------------------------------------------

ListActivity类的介绍(源码中的说明):

ListActivity has a default layout that consists of a single, full-screen list in the center of the screen. However, if you desire, you can customize the screen layout by setting your own view layout with setContentView() in onCreate(). To do this, your own view MUST contain a ListView object with the id "@android:id/list" (or android.R.id.list if it's in code) 

---------------------------------------------------------------------


在继承了ListActivity的类中填充ListView的信息时,不需要得到ListView对象,可以这样理解:当前activity就是一个ListView。

所以建好adapter之后,直接this.setListAdapter(adapter);就行了



给ListView添加行的点击监听:

复写onListItemClick()方法:

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		HashMap stuMap= (HashMap) l.getItemAtPosition(position);
		Toast.makeText(getApplicationContext(), stuMap.get("姓名").toString(), 1).show();
	}

其他细节:

1.如果要给ListView加滚动条

android:scrollbars="vertical"。或者,在ListView外面套一个ScrollView

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值