android ListView添加事件并获取选中项的值(转)

android ListView添加事件并获取选中项的值,ListView是一个经常用到的控件,ListView里面的每个子项Item可以使一个字符串,也可以是一个组合控件。

main.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="vertical" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <ListView 
  8.     android:id="@+id/myListView" 
  9.     android:layout_width="fill_parent" 
  10.     android:layout_height="wrap_content" 
  11.     /> 
  12. </LinearLayout> 

list_item.xml代码如下:

  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="horizontal" 
  4.     android:layout_width="fill_parent" 
  5.     android:layout_height="fill_parent" 
  6.     > 
  7. <TextView 
  8.     android:id="@+id/itemTitle" 
  9.     android:layout_width="wrap_content" 
  10.     android:layout_height="wrap_content" 
  11.     android:textSize="22dip" 
  12.     android:paddingRight="12dip" 
  13.     /> 
  14. <TextView 
  15.     android:id="@+id/itemContent" 
  16.     android:layout_width="wrap_content" 
  17.     android:layout_height="wrap_content" 
  18.     android:textSize="22dip" 
  19.     /> 
  20. </LinearLayout> 

activity MyListView.java代码如下:

  1. package listview.pack; 
  2.  
  3. import java.util.ArrayList; 
  4. import java.util.HashMap; 
  5. import android.app.Activity; 
  6. import android.os.Bundle; 
  7. import android.view.View; 
  8. import android.widget.AdapterView; 
  9. import android.widget.AdapterView.OnItemClickListener; 
  10. import android.widget.ListView; 
  11. import android.widget.SimpleAdapter; 
  12. import android.widget.Toast; 
  13.  
  14. public class MyListView extends Activity { 
  15.     /** Called when the activity is first created. */ 
  16.     //声明ListView对象 
  17.     ListView myListView; 
  18.     @Override 
  19.     public void onCreate(Bundle savedInstanceState) { 
  20.         super.onCreate(savedInstanceState); 
  21.         setContentView(R.layout.main); 
  22.         //生成ListView对象 
  23.         myListView=(ListView)findViewById(R.id.myListView); 
  24.         //创建ArrayList对象 并添加数据 
  25.         ArrayList<HashMap<String,String>> myArrayList=new ArrayList<HashMap<String,String>>(); 
  26.         for(int i=0;i<10;i++){ 
  27.             HashMap<String, String> map = new HashMap<String, String>(); 
  28.             map.put("itemTitle""This Is Title "+i); 
  29.             map.put("itemContent""This Is Content "+i); 
  30.             myArrayList.add(map); 
  31.         } 
  32.          
  33.         //生成SimpleAdapter适配器对象 
  34.         SimpleAdapter mySimpleAdapter=new SimpleAdapter(this
  35.                 myArrayList,//数据源 
  36.                 R.layout.list_items,//ListView内部数据展示形式的布局文件listitem.xml 
  37.                 new String[]{"itemTitle","itemContent"},//HashMap中的两个key值 itemTitle和itemContent 
  38.                 new int[]{R.id.itemTitle,R.id.itemContent});/*布局文件listitem.xml中组件的id   
  39.                                                             布局文件的各组件分别映射到HashMap的各元素上,完成适配*/ 
  40.          
  41.         myListView.setAdapter(mySimpleAdapter); 
  42.         //添加点击事件 
  43.         myListView.setOnItemClickListener(new OnItemClickListener(){ 
  44.             @Override 
  45.             public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
  46.                     long arg3) { 
  47.                 //获得选中项的HashMap对象 
  48.                 HashMap<String,String> map=(HashMap<String,String>)myListView.getItemAtPosition(arg2); 
  49.                 String title=map.get("itemTitle"); 
  50.                 String content=map.get("itemContent"); 
  51.                 Toast.makeText(getApplicationContext(),  
  52.                         "你选择了第"+arg2+"个Item,itemTitle的值是:"+title+"itemContent的值是:"+content, 
  53.                         Toast.LENGTH_SHORT).show(); 
  54.             } 
  55.              
  56.         }); 
  57.     } 
posted @ 2012-02-17 15:08  o0寂寞的泡沫0o  阅读(6939)  评论(0编辑  收藏  举报