欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ListView编写 博客分类: android androidlistView

程序员文章站 2024-03-14 22:35:11
...

布局文件layout中:

一个是Activity的主布局文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
	<LinearLayout 
	android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
	    >
    <ListView 
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        ></ListView>
    </LinearLayout>
</LinearLayout>

 

一个是对应数据的布局文件:user.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
    
    <TextView 
        android:id="@+id/user_name"
        android:layout_width="wrap_content"
        android:layout_height="30dp"
        android:ems="15"
        />
    <TextView 
        android:id="@+id/user_ip"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

 

Activity 继承 LlistActivity 类 , 重写 onCreate,onListItemClick 方法

 

package com.example.listview;

import java.lang.reflect.Array;
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.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

public class Activity03 extends ListActivity {
	
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activity03);
        
        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>();
        
        map1.put("user_name", "zhangsan");
        map1.put("user_ip", "192.168.1.1");
        map2.put("user_name", "lisi");
        map2.put("user_ip", "192.168.1.2");
        map3.put("user_name", "wangwu");
        map3.put("user_ip", "192.168.1.3");
        
        list.add(map1);
        list.add(map2);
        list.add(map3);
        
        SimpleAdapter simple = new SimpleAdapter(this, list, R.layout.user, new String[]{"user_name","user_ip"}, 
        		new int[]{R.id.user_name,R.id.user_ip});
        
        setListAdapter(simple);
    }

    
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		// TODO Auto-generated method stub
		super.onListItemClick(l, v, position, id);
		if(id==0){
			Toast.makeText(this, "zhangsan", Toast.LENGTH_SHORT).show();
		}
		else if(id ==1){
			Toast.makeText(this, "lisi", Toast.LENGTH_SHORT).show();
		}
		else{
			Toast.makeText(this, "wangwu", Toast.LENGTH_SHORT).show();
		}
	}



	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_activity03, menu);
        return true;
    }
}

 

 

 

 

  • ListView编写
            
    
    博客分类: android androidlistView
  • 大小: 18.2 KB
相关标签: android listView