先来描述下我想实现的功能,长按条目后,选择删除,然后会弹出提示dialog,点击确定则将该条目从数据库中删除,同时视图上也随之被删除
相关代码
private SimpleAdapter restaurantListAdapter=null;
@Override
protected void onResume(){
super.onResume();
//利用simpleAdapter
restaurantListAdapter=new SimpleAdapter(this, getListForSimpleAdapter(), R.layout.restaurant_list_item,
new String[]{"restName","Tel"}, new int[]{R.id.titleTextView,R.id.telTextView});
getListView().setTextFilterEnabled(true);
getListView().setAdapter(restaurantListAdapter);
Logger.d("on ruseme");
}
getListForSimpleAdapter()方法(返回list)
private List<Map<String, Object>> getListForSimpleAdapter(){
List<Map<String,Object>> list=new ArrayList<Map<String, Object>>();
int n = locandrest.queryLocAndRestByLocId(locID, this).size();
Logger.d("The number of Restaurant with locID = "+locID+" is "+n);
Long restID=null;
String RestaurantName=null;
String TelNum =null;
for(int i = 0; i<n; i++){
restID= LocAndRest.queryLocAndRestByLocId(locID, this).get(i).getRestId();
Logger.d("restID is "+restID);
RestaurantName = restaurant.queryRestaurantById(restID, this).getName();
TelNum=restaurant.queryRestaurantById(restID, this).getRestTelNumber();
Map<String, Object> map = new HashMap<String, Object>();
map.put("restName", RestaurantName);
map.put("Tel", "Tel: "+TelNum);
map.put("restID", restID);
list.add(map);
}
return list;
}
dialog上删除按钮的监听
//删除按钮监听_____________________________未完成 有误(无法刷新)
protected DialogInterface.OnClickListener deleteRestButtonListener(){
return new DialogInterface.OnClickListener() {
@SuppressWarnings("unchecked")
public void onClick(DialogInterface dialog, int id) {
Map<String,Object> mapRest =(Map<String, Object>) restaurantListAdapter.getItem(positionOfItem);
positionId=(Long) mapRest.get("restID");
deleteRestaurant(positionId);