简单实用的列表ListView 事件

<!-- 主布局文件 -->

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    
    <TextView 
         android:layout_width="fill_parent"
    	 android:layout_height="wrap_content"
    	 android:id="@+id/info"
    	 android:textSize="25px"
    	 android:layout_gravity="center_horizontal"/>
    
    <ListView 
        android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:id="@+id/listview"
   		/>
    
</LinearLayout>
<!-- 列表的行布局文件 -->

<TableLayout
     xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
 	 >
 	 
    <TableRow >
        <ImageView 
             android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:src="@drawable/icon"/>
        
        <TextView 
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:id="@+id/_id"/>
    		
        <TextView 
            android:layout_width="wrap_content"
    		android:layout_height="wrap_content"
    		android:id="@+id/name"/>
        
    </TableRow>
    
    
</TableLayout>
/**
 * Activity文件
 * 
 * SimpleAdapter实现ListView列表的显示
 * 
 * 		public SimpleAdapter (Context context, List<? extends Map<String, ?>> data,int resource, String[] from, int[] to)
 * 		listview.setAdapter(SimpleAdapter);
 *
 * AdapterView.OnItemClickListener接口实现监听
 * 		listview.setOnItemClickListener(new AdapterView.OnItemClickListener(){})
 * 
 */
package com.example.nima;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.HorizontalScrollView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.TimePicker;


public class CustomerDialog extends Activity{

	private String[][] data=new String[][]{
			{"001","我是****"},{"002","我的邮箱是@"},
			{"003","我的电话是111"},{"004","我的年龄是12"},
			{"005","我的专业是计算机"},{"006","我的网址是www"}};

	private SimpleAdapter adapter=null;
	private ListView listview=null;
	private TextView info=null;
	private List<Map<String,String>> list=null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		super.setContentView(R.layout.dialog_list);
		this.listview=(ListView)super.findViewById(R.id.listview);
		this.list=new ArrayList<Map<String,String>>();
		this.info=(TextView)super.findViewById(R.id.info);
		
		for(int i=0;i<data.length;i++){
			Map<String, String> map = new HashMap<String, String>();
			map.put("_id", this.data[i][0]);//把行布局要显示的元素封装成map
			map.put("name", this.data[i][1]);
			this.list.add(map);
		
		}
this.adapter=new SimpleAdapter(CustomerDialog.this, CustomerDialog.this.list, R.layout.tablelayout, //行布局文件
		new String[]{"_id","name"},new int[]{R.id._id,R.id.name});
		
		this.listview.setAdapter(this.adapter);
		
		this.listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

			/**
			 * onItemClick(AdapterView<?> parent, View view, int position, long id)
			 * parent	The AdapterView where the click happened.
			 * view	   The view within the AdapterView that was clicked (this will be a view provided by the adapter)
			 * position	  The position of the view in the adapter.
			 * id	   The row id of the item that was clicked.
			 */
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int position,long id) {
				// TODO Auto-generated method stub
				//取得单击之后的内容
				Map<String,String> map=(Map<String,String>)CustomerDialog.this.adapter.getItem(position);
				//取得数据
				String _id=map.get("_id");
				String name=map.get("name");
				CustomerDialog.this.info.setText("选中的数据项id是:"+_id+"  内容是:"+name);
			}
		});
	}
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值