android ListView添加事件並獲取選中項的值,ListView是一個經常用到的控件,ListView里面的每個子項Item可以使一個字符串,也可以是一個組合控件。
main.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <ListView
- android:id="@+id/myListView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
list_item.xml代碼如下:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <TextView
- android:id="@+id/itemTitle"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="22dip"
- android:paddingRight="12dip"
- />
- <TextView
- android:id="@+id/itemContent"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:textSize="22dip"
- />
- </LinearLayout>
activity MyListView.java代碼如下:
- package listview.pack;
- import java.util.ArrayList;
- import java.util.HashMap;
- import android.app.Activity;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- import android.widget.SimpleAdapter;
- import android.widget.Toast;
- public class MyListView extends Activity {
- /** Called when the activity is first created. */
- //聲明ListView對象
- ListView myListView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- //生成ListView對象
- myListView=(ListView)findViewById(R.id.myListView);
- //創建ArrayList對象 並添加數據
- ArrayList<HashMap<String,String>> myArrayList=new ArrayList<HashMap<String,String>>();
- for(int i=0;i<10;i++){
- HashMap<String, String> map = new HashMap<String, String>();
- map.put("itemTitle", "This Is Title "+i);
- map.put("itemContent", "This Is Content "+i);
- myArrayList.add(map);
- }
- //生成SimpleAdapter適配器對象
- SimpleAdapter mySimpleAdapter=new SimpleAdapter(this,
- myArrayList,//數據源
- R.layout.list_items,//ListView內部數據展示形式的布局文件listitem.xml
- new String[]{"itemTitle","itemContent"},//HashMap中的兩個key值 itemTitle和itemContent
- new int[]{R.id.itemTitle,R.id.itemContent});/*布局文件listitem.xml中組件的id
- 布局文件的各組件分別映射到HashMap的各元素上,完成適配*/
- myListView.setAdapter(mySimpleAdapter);
- //添加點擊事件
- myListView.setOnItemClickListener(new OnItemClickListener(){
- @Override
- public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
- long arg3) {
- //獲得選中項的HashMap對象
- HashMap<String,String> map=(HashMap<String,String>)myListView.getItemAtPosition(arg2);
- String title=map.get("itemTitle");
- String content=map.get("itemContent");
- Toast.makeText(getApplicationContext(),
- "你選擇了第"+arg2+"個Item,itemTitle的值是:"+title+"itemContent的值是:"+content,
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }