Android开发--控件ListView

来源:互联网 发布:拉塞尔场均数据 编辑:程序博客网 时间:2024/04/30 12:07

整体布局

<?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的id写法 android:id="@id/android:list"   或 “@android:id/list”  -->

<ListView

android:id="@id/android:list"  android:layout_width="fill_parent"  android:layout_height="wrap_content"

android:drawSelectorOnTop="false"  android:scrollbars="vertical"

/>

</LinearLayout>

</LinearLayout>

 

list布局:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    android:orientation="horizontal"  android:layout_width="fill_parent"    android:layout_height="fill_parent"

    >

<TextView  

    android:id="@+id/username"    android:layout_width="180dip"     android:layout_height="30dip" 

    android:textSize="10pt"    android:singleLine="true"

    />

    <TextView  

    android:id="@+id/ip"    android:layout_width="fill_parent" 

    android:layout_height="fill_parent"     android:gravity="right"

    android:textSize="10pt"

    />

</LinearLayout>

 

源程序:

package com.yajt.component;

import java.util.ArrayList;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import android.app.Activity;

import android.app.ListActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.ListView;

import android.widget.SimpleAdapter;

public class ListViewActivity extends ListActivity {

    /** Called when the activity is first created. */

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);        

        List<Map<String, String>> list = new ArrayList<Map<String,String>>();

        Map<String, String> map1 = new HashMap<String, String>();

        Map<String, String> map2 = new HashMap<String, String>();

        Map<String, String> map3 = new HashMap<String, String>();        

        map1.put("username", "zhangsan");

        map1.put("ip", "192.168.0.1");      

        map2.put("username", "wag");

        map2.put("ip", "192.168.0.2");        

        map3.put("username", "lizhao");

        map3.put("ip", "192.168.0.3");      

        list.add(map1);

        list.add(map3);

        list.add(map2);        

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

        setListAdapter(listAdapter);

    }

@Override

protected void onListItemClick(ListView l, View v, int position, long id) {

// TODO Auto-generated method stub

System.out.println("id ----" + id);

System.out.println("position ---" + position);

super.onListItemClick(l, v, position, id);

}    

}

 

原创粉丝点击