简单实用的列表ListView 事件

来源:互联网 发布:淘宝质量问题 编辑:程序博客网 时间:2024/05/17 22:53
<!-- 主布局文件 --><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;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.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]);//把行布局要显示的元素封装成mapmap.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) * parentThe 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. */@Overridepublic 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);}});}}



0 0
原创粉丝点击