android ListView 使用 示例_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > android ListView 使用 示例

android ListView 使用 示例

 2010/11/26 8:07:19  byandby  http://byandby.javaeye.com  我要评论(0)
  • 摘要:什么是listview?直白点说就是一个能垂直滚动显示列表项的视图View这些选项的数据来自与这个ListView关联的ListAdapter这个示例需要2个布局文件man.xml这个布局文件用来设置Activity的布局<?xmlversion="1.0"encoding="utf-8"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android
  • 标签:ListView使用示例
    什么是listview?  直白点说就是一个能垂直滚动显示列表项的视图View 这些选项的数据来自与这个ListView关联的ListAdapter 这个示例需要2个布局文件

man.xml
这个布局文件用来设置Activity的布局

<?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_height="wrap_content"
  		android:orientation="vertical"
  		android:layout_width="fill_parent">
  		<!-- 我们会自己定义listview的显示方式(在另外一个布局文件里边)不用默认的方式 如果自定义listview的显示方式这里这个 
  		android:id="@id/android:list" 必须这样写 
  		 -->
  		<ListView android:id="@id/android:list" android:layout_width="fill_parent" 
  			android:layout_height="wrap_content" android:drawSelectorOnTop="false"
  			android:scrollbars="vertical"/>
  			<!-- android:drawSelectOnTop="false"此属性用来设置listview上的背景颜色会不会挡住(覆盖)内容
  				如果这是为false就表示不会覆盖掉 ,这个大家拿例子测试一下效果就明白了
  			 -->
  </LinearLayout>
</LinearLayout>


user.xml
user.xml用来设置listview的布局方式 在白点说就是 你想怎么显示这个listView

<?xml version="1.0" encoding="utf-8"?>
<!-- 此布局文件用来定义listview 的显示方式 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="10dip"
    android:paddingRight="10dip"
    android:paddingTop="1dip"
    android:paddingBottom="1dip"
    >
<TextView  
	android:id="@+id/user_name"
    android:layout_width="180dip" 
    android:layout_height="30dip"
    android:textSize="10pt"
    android:singleLine="true"/> 
<TextView 
	android:id="@+id/user_ip" 
	android:layout_width="fill_parent"
	android:layout_height="fill_parent"	
	android:gravity="right"
	android:textSize="10pt"/> 
</LinearLayout>

Activity类 注意这个Activity01继承的是ListActivity
package xiaohang.zhimeng;

import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class Activity01 extends ListActivity {
   
	//ListActivity一个以列表的方式显示数据源、数组的Activity
	//ListActivity Class Overview(此描述摘自官方文档说的非常清楚了)
	/*An activity that displays a list of items by binding to a data source such as an array or Cursor, and exposes event handlers when the user selects an item.

	ListActivity hosts a ListView object that can be bound to different data sources, typically either an array or a Cursor holding query results. Binding, screen layout, and row layout are discussed in the following sections.

	Screen Layout

	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 list if it's in code)

	Optionally, your custom view can contain another view object of any type to display when the list view is empty. This "empty list" notifier must have an id "android:empty". Note that when an empty view is present, the list view will be hidden when there is no data to display.

	The following code demonstrates an (ugly) custom screen layout. It has a list with a green background, and an alternate red "no data" message.*/
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.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>();
        //一个map对象对应一条数据
        map1.put("user_name", "zhangsan");
        map1.put("user_ip", "192.168.0.1");
        
        map2.put("user_name", "lisi");
        map2.put("user_ip", "192.168.0.2");
        
        map3.put("user_name", "wangwu");
        map3.put("user_ip", "192.168.0.3");
        list.add(map1);
        list.add(map2);
        list.add(map3);
        //这里对SimpleAdapter这个构造方法的参数说明一下 E文好的直接看E文
        /*context	The context where the View associated with this SimpleAdapter is running
        data	A List of Maps. Each entry in the List corresponds to one row in the list. The Maps contain the data for each row, and should include all the entries specified in "from"
        resource	Resource identifier of a view layout that defines the views for this list item. The layout file should include at least those named views defined in "to"
        from	A list of column names that will be added to the Map associated with each item.
        to	The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter.*/
        /**
         * 参数一 Context 这个不说了
         * 参数二 就是上边声明的那个ArrayList对象
         * 参数三 这个参数用来指定 我们一行数据 的key 也就是一个map对象的key 上下结合看一下 因为我们一条数据也就是一行
         * 对应一个map对象 一个map对象包含2个数据 即 user_name 和 user_ip 这个参数就是用来指定这2个key 这里是通过String数组的方式
         * 参数四  大家一看就知道了 意思是 user_name 这条数据用 R.id.user_name 这个TextView显示  user_ip 这条数据用 
         * R.id.user_ip 显示
         */
        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});
        //这是Adapter setListAdapter()此方法来自ListActivity
        setListAdapter(listAdapter);
    }
    //当我们点击一条数据 或者说一行时 触发的Click事件
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    	super.onListItemClick(l, v, position, id);
    	//我们输出它的 ID 和 position
    	//ID
    	System.out.println("id------------>" + id);
    	//位置
    	System.out.println("position--------->" + position);
    }
}

ANDROID 2.0  APILEVEL 5
源码和运行效果图见附件
  • 相关文章
发表评论
用户名: 匿名