先来描述下我想实现的功能,长按条目后,选择删除,然后会弹出提示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);
String str = "The restaurant which restId (positionId) is "+positionId+"is deleted in Restaurant Table";
Logger.d(str);
getListForSimpleAdapter().remove(positionOfItem); Logger.d("the size of list is "+getListForSimpleAdapter().size());
SimpleAdapter sa= (SimpleAdapter) getListView().getAdapter();
sa.notifyDataSetChanged();
removeDialog(CONFIRM_DELETE_DIALOG);//清除缓存
}
};
}但是运行后发现,条目的确是被删除了(从数据库中可以查到),或者后退重新进入该activity可以看出被删除了
但是在操作的时候(即在dialog上点击删除)并没有对应刷新列表,
该怎么解决呢 或者是我哪里写的有误,请指点
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);
String str = "The restaurant which restId (positionId) is "+positionId+"is deleted in Restaurant Table";
Logger.d(str);
getListForSimpleAdapter().remove(positionOfItem); Logger.d("the size of list is "+getListForSimpleAdapter().size());
SimpleAdapter sa= (SimpleAdapter) getListView().getAdapter();
sa.notifyDataSetChanged();
removeDialog(CONFIRM_DELETE_DIALOG);//清除缓存
}
};
}但是运行后发现,条目的确是被删除了(从数据库中可以查到),或者后退重新进入该activity可以看出被删除了
但是在操作的时候(即在dialog上点击删除)并没有对应刷新列表,
该怎么解决呢 或者是我哪里写的有误,请指点
解决方案 »
- 我程序里面一个服务的代码,在模拟器上运行的,为什么getLastKnownLocation总是返回空值呢?麻烦帮我看看
- cygwin下repo下载 android出错!!刚开始玩,请大家指教!
- 把Activity放到Gallery中的焦点问题
- android模拟器启动出错
- 哪位哥哥写一个自定义ActionBar的demo给看看
- android判断fm 在线等
- android 可执行文件怎么集成到系统中?
- Android listview和其父view(groupview)手势如何分离
- 关于android GPS设置
- 关于安卓调用C#的WebService上传图片问题(不使用ksoap2)
- 一直报source not found错误
- android2.2开发时在eclipse的file explorer视图中不显示手机的sdcard信息
所以这样试下:
在主activity中建立一个handler,然后把dialog里面的sa.notifyDataSetChanged();改成发送消息到actvity,在handle的处理中调用notifyDataSetChanged();
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case REFRESH_LISTVIEW:
Logger.d("call the handler to refresh listview");
/* SimpleAdapter sa= (SimpleAdapter) getListView().getAdapter();
sa.notifyDataSetChanged();*/
restaurantListAdapter=new SimpleAdapter(RestaurantList.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);
break;
default:
Logger.d("unknown message to handler");
break;
}
}
};删除按钮消息发送部分
Message message = new Message();
message.what=REFRESH_LISTVIEW;
handler.sendMessage(message); 我在处理消息的方法里开始用的是
SimpleAdapter sa= (SimpleAdapter) getListView().getAdapter();
sa.notifyDataSetChanged()
也就是注释掉的部分,没效果
后来改成重新new 了下才有效果
也就是说notifyDataSetChanged这个实际上没有起作用啊 这是为什么呢 还是我哪里有误
麻烦再看看了 多谢