首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > Android >

关于simpleAdapter刷新的有关问题(怎么利用notifyDataSetChanged())

2013-06-19 
关于simpleAdapter刷新的问题(如何利用notifyDataSetChanged())先来描述下我想实现的功能,长按条目后,选择

关于simpleAdapter刷新的问题(如何利用notifyDataSetChanged())
先来描述下我想实现的功能,长按条目后,选择删除,然后会弹出提示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上点击删除)并没有对应刷新列表,
该怎么解决呢 或者是我哪里写的有误,请指点
[解决办法]
notifyDataSetChanged()中会去对做UI的刷新动作,这个动作最好放在对应的UI线程中。
所以这样试下:
在主activity中建立一个handler,然后把dialog里面的sa.notifyDataSetChanged();改成发送消息到actvity,在handle的处理中调用notifyDataSetChanged();

[解决办法]
引用:
notifyDataSetChanged()中会去对做UI的刷新动作,这个动作最好放在对应的UI线程中。
所以这样试下:
在主activity中建立一个handler,然后把dialog里面的sa.notifyDataSetChanged();改成发送消息到actvity,在handle的处理中调用notifyDataSetChanged();


支持

热点排行