int 和integer的误用导致 listview的不刷新
2015-11-11 15:43
309 查看
今天犯了一个低级的错误,对int 和integer的使用没有正确。
遇到的情况是这样的:我要刷新listview,可是数据已经重新设置,就是没有刷新,当时数据是这样处理的:
private Map<Integer, MusicInfo> mSelectMap;
public void removeDeleteData(List<MusicInfo> musicInfos) {
// TODO Auto-generated method stub
Iterator iterator = mSelectMap.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry e=(Map.Entry)iterator.next();
Integer key = (Integer) e.getKey();
musicInfos.remove(key.intValue());
System.out.println(e.getKey()+"="+e.getValue()+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
Log.d("pzc", musicInfos.size() + "<<<<<<<<<<<<<<<<<<<<");
mMusicInfos = musicInfos;
//notifyDataSetChanged();
}}
目的就是要重原有的musicInfos数据中删除mSelectMap中有的数据,但是上面的代码有什么问题呢?
问题就在于
Integer key = (Integer) e.getKey();
musicInfos.remove(key.intValue());
musicInfos.remove(int arg0)和musicInfos.remove(object arg0);两种方法,我们要的是musicInfos.remove(int arg0)这个,而使用上面代码走的却是musicInfos.remove(object arg0),导致我的数据一直保持原样,没有更新,为什么呢?
因为integer是int的封装,它是一个对象。
java.lang.Integer是一个类.对它的操作要通过类的方法
int是JAVA缺省的8中基本数据类型之一.不是类的对象.
int是基本数据类型,Integer是对int进行了封装的一个类。
遇到的情况是这样的:我要刷新listview,可是数据已经重新设置,就是没有刷新,当时数据是这样处理的:
private Map<Integer, MusicInfo> mSelectMap;
public void removeDeleteData(List<MusicInfo> musicInfos) {
// TODO Auto-generated method stub
Iterator iterator = mSelectMap.entrySet().iterator();
while(iterator.hasNext()){
Map.Entry e=(Map.Entry)iterator.next();
Integer key = (Integer) e.getKey();
musicInfos.remove(key.intValue());
System.out.println(e.getKey()+"="+e.getValue()+"<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
}
Log.d("pzc", musicInfos.size() + "<<<<<<<<<<<<<<<<<<<<");
mMusicInfos = musicInfos;
//notifyDataSetChanged();
}}
目的就是要重原有的musicInfos数据中删除mSelectMap中有的数据,但是上面的代码有什么问题呢?
问题就在于
Integer key = (Integer) e.getKey();
musicInfos.remove(key.intValue());
musicInfos.remove(int arg0)和musicInfos.remove(object arg0);两种方法,我们要的是musicInfos.remove(int arg0)这个,而使用上面代码走的却是musicInfos.remove(object arg0),导致我的数据一直保持原样,没有更新,为什么呢?
因为integer是int的封装,它是一个对象。
java.lang.Integer是一个类.对它的操作要通过类的方法
int是JAVA缺省的8中基本数据类型之一.不是类的对象.
int是基本数据类型,Integer是对int进行了封装的一个类。
相关文章推荐
- 关于setClickable(false)失效的问题
- 移动硬盘“脱机”(签名冲突)的硬盘如何正常使用
- zookeeper简单配置使用
- Java word文件创建
- java进阶-创建和销毁对象3
- 实战 Windows 10 Microsoft Edge 中的多媒体投影功能
- 栈应用2:走迷宫
- POJ 2947:Widget Factory 求同余方程
- 设备检测,手机建站框架
- 配置JQuery与其它库的兼容性
- Tomcat 配置续
- Murano Weekly Meeting 2015.11.11
- LibLinear(SVM包)使用说明之(一)README
- POJ 2947:Widget Factory 求同余方程
- 使用php获取本月、上周、昨日、今日的起始、结束时间戳的方法
- linux 搭建memcached服务
- 我和postgreSQL的二三事
- android编程实现局部界面动态切换的方法
- [LeetCode]Perfect Squares
- [LeetCode]Perfect Squares