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.     } 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM