packagecom.ray.test;
importjava.util.ArrayList;
importjava.util.HashMap;
importandroid.app.Activity;
importandroid.os.Bundle;
importandroid.view.ContextMenu;
importandroid.view.MenuItem;
importandroid.view.View;
importandroid.view.ContextMenu.ContextMenuInfo;
importandroid.view.View.OnCreateContextMenuListener;
importandroid.widget.AdapterView;
importandroid.widget.ListView;
importandroid.widget.SimpleAdapter;
importandroid.widget.AdapterView.OnItemClickListener;
publicclassTestListViewextendsActivity{
@Override
publicvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//綁定Layout裡面的ListView
ListViewlist=(ListView)findViewById(R.id.ListView01);
//生成動態數組,加入數據
ArrayList>listItem=newArrayList>();
for(inti=0;i<10;i++)
{
HashMapmap=newHashMap();
map.put("ItemImage",R.drawable.checked);//圖像資源的ID
map.put("ItemTitle","Level"+i);
map.put("ItemText","Finishedin1Min54Secs,70Moves!");
listItem.add(map);
}
//生成適配器的Item和動態數組對應的元素
SimpleAdapterlistItemAdapter=newSimpleAdapter(this,listItem,//數據源
R.layout.list_items,//ListItem的XML實現
//動態數組與ImageItem對應的子項
newString[]{"ItemImage","ItemTitle","ItemText"},
//ImageItem的XML檔案裡面的一個ImageView,兩個TextViewID
newint[]{R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}
);
//添加並且顯示
list.setAdapter(listItemAdapter);
//添加點擊
list.setOnItemClickListener(newOnItemClickListener(){
@Override
publicvoidonItemClick(AdapterViewarg0,Viewarg1,intarg2,
longarg3){
setTitle("點擊第"+arg2+"個項目");
}
});
//添加長按點擊
list.setOnCreateContextMenuListener(newOnCreateContextMenuListener(){
@Override
publicvoidonCreateContextMenu(ContextMenumenu,Viewv,ContextMenuInfomenuInfo){
menu.setHeaderTitle("長按選單-ContextMenu");
menu.add(0,0,0,"彈出長按選單0");
menu.add(0,1,0,"彈出長按選單1");
}
});
}
//長按選單回響函式
@Override
publicbooleanonContextItemSelected(MenuItemitem){
setTitle("點擊了長按選單裡面的第"+item.getItemId()+"個項目");
returnsuper.onContextItemSelected(item);
}
}
1.packagecom.ray.test;
2.3.importjava.util.ArrayList;
4.importjava.util.HashMap;
5.6.importandroid.app.Activity;
7.importandroid.os.Bundle;
8.importandroid.view.ContextMenu;
9.importandroid.view.MenuItem;
10.importandroid.view.View;
11.importandroid.view.ContextMenu.ContextMenuInfo;
12.importandroid.view.View.OnCreateContextMenuListener;
13.importandroid.widget.AdapterView;
14.importandroid.widget.ListView;
15.importandroid.widget.SimpleAdapter;
16.importandroid.widget.AdapterView.OnItemClickListener;
17.18.publicclassTestListViewextendsActivity{
19.@Override
20.publicvoidonCreate(BundlesavedInstanceState){
21.super.onCreate(savedInstanceState);
22.setContentView(R.layout.main);
23.//綁定Layout裡面的ListView
24.ListViewlist=(ListView)findViewById(R.id.ListView01);
25.26.//生成動態數組,加入數據
27.ArrayList>listItem=newArrayList>();
28.for(inti=0;i<10;i++)
29.{
30.HashMapmap=newHashMap();
31.map.put("ItemImage",R.drawable.checked);//圖像資源的ID
32.map.put("ItemTitle","Level"+i);
33.map.put("ItemText","Finishedin1Min54Secs,70Moves!");
34.listItem.add(map);
35.}
36.//生成適配器的Item和動態數組對應的元素
37.SimpleAdapterlistItemAdapter=newSimpleAdapter(this,listItem,//數據源
38.R.layout.list_items,//ListItem的XML實現
39.//動態數組與ImageItem對應的子項
40.newString[]{"ItemImage","ItemTitle","ItemText"},
41.//ImageItem的XML檔案裡面的一個ImageView,兩個TextViewID
42.newint[]{R.id.ItemImage,R.id.ItemTitle,R.id.ItemText}
43.);
44.45.//添加並且顯示
46.list.setAdapter(listItemAdapter);
47.48.//添加點擊
49.list.setOnItemClickListener(newOnItemClickListener(){
50.51.@Override
52.publicvoidonItemClick(AdapterViewarg0,Viewarg1,intarg2,
53.longarg3){
54.setTitle("點擊第"+arg2+"個項目");
55.}
56.});
57.58.//添加長按點擊
59.list.setOnCreateContextMenuListener(newOnCreateContextMenuListener(){
60.61.@Override
62.publicvoidonCreateContextMenu(ContextMenumenu,Viewv,ContextMenuInfomenuInfo){
63.menu.setHeaderTitle("長按選單-ContextMenu");
64.menu.add(0,0,0,"彈出長按選單0");
65.menu.add(0,1,0,"彈出長按選單1");
66.}
67.});
68.}
69.70.//長按選單回響函式
71.@Override
72.publicbooleanonContextItemSelected(MenuItemitem){
73.setTitle("點擊了長按選單裡面的第"+item.getItemId()+"個項目");
74.returnsuper.onContextItemSelected(item);
75.}
76.}