package com.example.l0909_4_listview_all;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnCreateContextMenuListener;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.Toast;
public class MainActivity extends Activity {
private ListView lv;
private SimpleAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView) findViewById(R.id.lv);
//用适配器将数据是配到控件的过程
adapter = new SimpleAdapter(MainActivity.this, getData(),
R.layout.cell, new String[] { "Title", "Message" }, new int[] {
R.id.tv_title, R.id.tv_mess });
lv.setAdapter(adapter);
// AdapterView的点击事件
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
setTitle("点击第" +( arg2+1) + "个项目");
Toast.makeText(MainActivity.this, "点击第" + (arg2+1) + "个项目",
Toast.LENGTH_LONG).show();
}
});
// AdapterView的长按点击事件,这里我们把它注释掉, 因为其优先级高于带有子菜单的长按点击事件。
lv.setOnItemLongClickListener(new OnItemLongClickListener() {
@Override public boolean onItemLongClick(AdapterView<?> arg0, View
arg1, int arg2, long arg3) { setTitle("点击第" + arg2 + "个项目");
Toast.makeText(MainActivity.this, "长按点击第" + arg2 + "个项目",
Toast.LENGTH_LONG).show();
return true; } });
// AdapterView的带有子菜单选项的长按点击事件
lv.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.setHeaderIcon(R.drawable.p5); // 字义图片样式
menu.setHeaderTitle("长按菜单选项:");
menu.add(0, 1, 0, "删除");
menu.add(0, 2, 0, "查看");
}
});
}
/**
* 当上下文菜单子选项被选中时自动调用的方法
*/
@Override
public boolean onContextItemSelected(MenuItem item) {
// 得到出现菜单项的对应的ListView的position
int selectedPosition = ((AdapterContextMenuInfo) item.getMenuInfo()).position;
// 得到ListView中对应position位置的数据
final Map<String, String> map = (HashMap) lv.getItemAtPosition(selectedPosition);
switch (item.getItemId()) {
case 1:
new AlertDialog.Builder(this)
.setTitle(map.get("Title"))
.setMessage(map.get("确定删除选项吗?"))
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
map.remove("Title");
map.remove("Message");
//删除之后刷新数据源重新适配数据
adapter.notifyDataSetChanged();
}
}).setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
break;
case 2:
new AlertDialog.Builder(this)
.setTitle(map.get("Title"))
.setMessage(map.get("Message"))
.setPositiveButton("确定",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
}).show();
break;
}
return super.onContextItemSelected(item);
}
/**
* 添加数据源
*
* @return
*/
private List<Map<String, String>> getData() {
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Map<String, String> map1 = new HashMap<String, String>();
Map<String, String> map2= new HashMap<String, String>();
Map<String, String> map3 = new HashMap<String, String>();
Map<String, String> map4 = new HashMap<String, String>();
Map<String, String> map5 = new HashMap<String, String>();
Map<String, String> map6 = new HashMap<String, String>();
map1.put("Title", "王剑" );
map1.put("Message", "18823450968" );
map2.put("Title", "张菲" );
map2.put("Message", "13278904678" );
map3.put("Title", "李斯" );
map3.put("Message", "15123458905" );
map4.put("Title", "塔诺" );
map4.put("Message", "13789463869" );
map5.put("Title", "狗狗" );
map5.put("Message", "18747389046" );
map6.put("Title", "刘老师" );
map6.put("Message", "18009382758" );
list.add(map1);
list.add(map2);
list.add(map3);
list.add(map4);
list.add(map5);
list.add(map6);
return list;
}
}
(1)单击第一项:
(2)长按列表选项:
(3)点击对菜单项中的"查看"弹出的对话框:
(4)点击对菜单项中的"删除"弹出的对话框:
(5)点击删除的确定按钮后更新的ListView列表:
转载于:https://blog.51cto.com/wangzhaoli/1293420