ListView实例

在Android中,ListView是用来显示一个列表的控件。每一行列表都是一个独立的元素。这种控件既可以方便的显示从系统中其他应用读取出来的数据,也可独立的为各行元素设置监听器。

首先先看代码吧。。。


MainActivity.xml

package com.example.progressbardemo;

import java.util.ArrayList;
import java.util.HashMap;

import android.os.Bundle;
import android.app.Activity;
import android.app.ListActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleAdapter;

public class MainActivity extends ListActivity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
		HashMap<String,String> map1=new HashMap<String,String>();
		HashMap<String,String> map2=new HashMap<String,String>();
		HashMap<String,String> map3=new HashMap<String,String>();
		HashMap<String,String> map4=new HashMap<String,String>();
		HashMap<String,String> map5=new HashMap<String,String>();
		HashMap<String,String> map6=new HashMap<String,String>();
		HashMap<String,String> map7=new HashMap<String,String>();
		
		map7.put("user_name", "名字");
		map7.put("user_ip", "IP");
		
		map1.put("user_name", "马建杰1");
		map1.put("user_ip", "192.168.1.1");
		
		map2.put("user_name", "小三1");
		map2.put("user_ip", "192.168.1.2");
		
		map3.put("user_name", "崔力1");
		map3.put("user_ip", "192.168.1.3");
		
		map4.put("user_name", "马建杰2");
		map4.put("user_ip", "192.168.1.4");
		
		map5.put("user_name", "小三2");
		map5.put("user_ip", "192.168.1.5");
		
		map6.put("user_name", "崔力2");
		map6.put("user_ip", "192.168.1.6");
		
		list.add(map7);
		list.add(map1);
		list.add(map2);
		list.add(map3);
		list.add(map4);
		list.add(map5);
		list.add(map6);
		
		
		SimpleAdapter listAdapter = new SimpleAdapter(this, 
				list, R.layout.user, 
				new String[]{"user_name","user_ip"}, 
				new int[]{R.id.user_name,R.id.user_ip});
		
		setListAdapter(listAdapter);
	}

	
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		System.out.println("id--------------"+id);
		System.out.println("position--------------"+position);
		

	}



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
	
	

}




新建一个user.xml的布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
	android:orientation="horizontal"
	android:paddingLeft="10dip"	
	android:paddingRight="10dip"
	android:paddingTop="1dip"
	android:paddingBottom="1dip"		
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"
	>
	<!-- 这里定义LinearLayout为水平的,并且这里定义两个textview,一个存放name一个存放ip -->
   <TextView 
       android:id="@+id/user_name"
       android:layout_width="100dip"
       android:layout_height="30dip"
       android:textSize="10pt"
       android:singleLine="true"
       />
     <TextView 
       android:id="@+id/user_ip"
       android:layout_width="200dip"
       android:layout_height="30dip"
       android:textSize="10pt"
       android:gravity="center"
       android:singleLine="true"
       />
     
</LinearLayout>

activity_main.xml的代码:

<?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"
	>
   <LinearLayout 
       android:id="@+id/listLinearLayout"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:orientation="vertical">
       <ListView
           android:id="@id/android:list"
           android:layout_width="fill_parent"
           android:layout_height="wrap_content"
           android:drawSelectorOnTop="false"
           android:scrollbars="vertical"
           />
       
       </LinearLayout> 
   <!-- android:drawSelectorOnTop="true"  点击某一条记录,颜色会显示在最上面,记录上的文字被遮住,所以点击文字不放,文字就看不到
		android:drawSelectorOnTop="false" 点击某条记录不放,颜色会在记录的后面,成为背景色,但是记录内容的文字是可见的
	-->
</LinearLayout>

SimpleAdapter这是一个简单的适配器,可以将静态数据映射到XML文件中定义好的视图。你可以指定数据支持的列表如ArrayList组成的Map。

SimpleAdapter listAdapter = new SimpleAdapter(this, 
list, R.layout.user, 
new String[]{"user_name","user_ip"}, 
new int[]{R.id.user_name,R.id.user_ip});

这里的new String[]{"user_name","user_ip"}中的user_name和user_ip是在user.xml 中定义的,下面的new int[]{R.id.user_name,R.id.user_ip}

中的是根据键值找到对应的value。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值