ListView新增事件並獲取選中項的值

不淨之心發表於2012-03-12
[url]http://www.beijibear.com/?aid=341[/url]
為什麼ListView.setOnItemClickListener、setOnCreateContextMenuListener會無效:
[url]http://blog.csdn.net/mahaiyun18/article/details/6531371[/url]
如果ListView中的單個Item的view中存在checkbox,button等view,會導致ListView.setOnItemClickListener無效,
事件會被子View捕獲到,ListView無法捕獲處理該事件.
解決方法:
在checkbox、button對應的view處加android:focusable="false"
android:clickable="false" android:focusableInTouchMode="false"
其中focusable是關鍵


Android ListView/ListActivity點選長按事件:
[url]http://blog.163.com/zmhot88@126/blog/static/16984664720107319215488/[/url]
ListActivity和ListView是很常用的元件,用來製作列表形式的使用者介面。本文介紹如何正確處理ListView中的條目短按和長 按事件,他們的處理方式是不同的。

  對於短按事件,處理起來比較簡單,我們只需要覆蓋ListActivity的onListItemClick()方法,如下所示:

@Override
protected void onListItemClick(ListView arg0, View arg1, int arg2, long arg3) {
CharSequence s = ((TextView)arg1).getText();
Log.e("CallLogActivity",s+ " is clicked");
super.onListItemClick(arg0, arg1, arg2, arg3);
}

  對於長按事件,我們需要給listview註冊一個OnItemLongClickListener,並實現Listener中定義的方法,如 下所示:

  getListView().setOnItemLongClickListener(new OnItemLongCLickListener(){

public boolean onItemLongClick(AdapterView parent, View view, int position,
long id) {
Log.e("CallLogActivity", view.toString() + "position=" + position);
CharSequence number = ((TextView) view).getText();
Toast t = Toast.makeText(this, number + " is long clicked",
Toast.LENGTH_LONG);
t.show();
return true;
}

});



下面是例子程式碼。不包含上述技術
--------------------------------------------

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();
}

});
}
}

相關文章