关于simpleAdapter刷新的问题(如何利用notifyDataSetChanged())

jesse1989 jesse1989 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
先来描述下我想实现的功能,长按条目后,选择删除,然后会弹出提示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上点击删除)并没有对应刷新列表,
该怎么解决呢 或者是我哪里写的有误,请指点
...全文
评论 12
收藏 已收藏
分享
返回顶部
jesse1989
Android 80,490 成员 91,375 内容