在做一个通讯录的app,使用BaseAdapter作为adapter。重写了getCount()、getItem()、getItemId() 、getView()方法。

因为新建联系人在第二个activity,所以就把adapter的notifyDataSetChanged()方法放在了第一个activity的生命周期方法onResume()中。但是遇到了bug,就是把新的联系人添加到了数据库,但是返回到第一个activity的时候,listview的显示没有更新。原因是:adapter使用的数据是数据库变更前的数据。当数据库数据变更以后,数据库中的数据已经和内存中的数据不一致了。adapter的notifyDataSetChanged()方法查看到内存中的数据没有更新,所以listview也就不会更新了。

请教了高手怎么处理,目前我能实现的方法就是使用一个广播,在保存数据的时候,发送一个广播,然后在adapter初始化的时候接收广播,假如有广播的话,把内存中的数据清空,然后重新读取数据库的数据。

请看代码:

 public class MyAdapter extends BaseAdapter {

     private List<PhoneBean> lists;
private Context context; public MyAdapter(Context context){ this.lists = GetPhoneFromSQL.getPhoneInfo(context);
this.context=context; 11 IntentFilter intentFilter = new IntentFilter("com.lijingbo.getmyphonenumber.PHONE_SQL_CHANGED");
12 context.registerReceiver(new BroadcastReceiver() {
13 @Override
14 public void onReceive(Context context, Intent intent) {
15 lists.clear();
16 lists = GetPhoneFromSQL.getPhoneInfo(context);
17 notifyDataSetChanged();
18 }
19 } , intentFilter);
20 } @Override
public int getCount() {
return lists.size();
} @Override
public Object getItem(int position) {
return position;
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
if (convertView==null) {
holder=new ViewHolder();
convertView=LayoutInflater.from(context).inflate(R.layout.phonedetails, null);
holder.showName=(TextView) convertView.findViewById(R.id.showName);
convertView.setTag(holder);
}else {
holder=(ViewHolder) convertView.getTag();
}
holder.showName.setText(lists.get(position).getName());
notifyDataSetChanged();
return convertView;
} static class ViewHolder{
public TextView showName;
} }

红色字体为新加的广播。

保存数据的部分:

 public class SavePhoneToSQL {

     private static final String DBNAME="Phones";
private static PhoneBean phoneBean;
private static DbUtils db;
static List<PhoneBean> lists ; public static void saveData(Context context,String name,String number,String company,String email) {
phoneBean = new PhoneBean();
phoneBean.setName(name);
phoneBean.setNumber(number);
phoneBean.setCompany(company);
phoneBean.setCompany(email); db = DbUtils.create(context);
db.configAllowTransaction(true);
db.configDebug(true);
try {
db.createTableIfNotExist(PhoneBean.class);
} catch (DbException e1) {
Toast.makeText(context, "创建数据库失败", Toast.LENGTH_SHORT).show();
}
try {
24 db.save(phoneBean);
25 Intent intent = new Intent();
26 intent.setAction("com.lijingbo.getmyphonenumber.PHONE_SQL_CHANGED");
27 context.sendBroadcast(intent);
} catch (DbException e) {
// TODO Auto-generated catch block
Toast.makeText(context, "数据保存失败", Toast.LENGTH_SHORT).show();
} } public static List<PhoneBean> getPhoneInfo() {
try {
lists=db.findAll(PhoneBean.class);
} catch (DbException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return lists;
}
}

Android开发之使用BaseAdapter的notifyDataSetChanged()无法更新列表的更多相关文章

  1. Android 开发环境搭建以及工具(不断更新)

    学习android需要学习的编程知识 https://wiki.cyanogenmod.org/w/Doc:_Development_Resources 从http://source.android. ...

  2. Android开发中常用的库总结(持续更新)

    这篇文章用来收集Android开发中常用的库,都是实际使用过的.持续更新... 1.消息提示的小红点 微信,微博消息提示的小红点. 开源库地址:https://github.com/stefanjau ...

  3. [转]Android开发要看的网站(不断更新中)

    Android网址或Blog Android官网 身为Android开发者不知道这个网站就太说不过去了,上面有你任何你需要的东西 Android Developers Blog Android官网博客 ...

  4. Android开发学习——ListView+BaseAdapter的使用

    ListView 就是用来显示一行一行的条目的MVC结构 * M:model模型层,要显示的数据           ----people集合 * V:view视图层,用户看到的界面          ...

  5. android开发中的BaseAdapter之理解(引用自网络,总结的很好,谢谢)

    android中的适配器(Adapter)是数据与视图(View)之间的桥梁,用于对要显示的数据进行处理,并通过绑定到组件进行数据的显示. BaseAdapter是Android应用程序中经常用到的基 ...

  6. Android开发——利用Cursor+CursorAdapter实现界面实时更新

    好久没有更新博客了.不是没时间写,而是太懒.而且感觉有些东西没有时间总结,之之后再想写,就想不起来了.晚上新发现一点东西,所以就及时写下来. 最近利用业余时间在看Android的Download模块, ...

  7. Android开发UI之在子线程中更新UI

    转自第一行代码-Android Android是不允许在子线程中进行UI操作的.在子线程中去执行耗时操作,然后根据任务的执行结果来更新相应的UI控件,需要用到Android提供的异步消息处理机制. 代 ...

  8. Android开发 - 下拉刷新和分段头悬停列表

    项目源码 本文所述项目已开源,源码地址 为什么做PullToRefresh-PinnedSection-ListView 前段时间因为项目需求,需要在Android中对ListView同时增加下拉刷新 ...

  9. Android开发 ViewPager删除Item后,不会更新数据和View

    问题描述: 在使用ViewPager的适配器删除适配器里一个Item后依然会,而删除的这个item依然会保留缓存,适配器不会重新加载更新数据.如下代码: public class TReleaseCi ...

随机推荐

  1. Spread 之自定义对角线cellType源码: DiagonalCellType

    最新的SpreadWinform提供了多达24种CellType类型,下面的这2篇博文对新增了GcTextBoxCellType和GcDateTimeCellType单元格格式做了比较详细的说明. & ...

  2. 关于修改tabbar的颜色的问题

    首先,项目是在故事板中搭建的,所以遇到这个问题的时候,首先是想到在故事板中找到相关的属性,确实是有一个Selected Image,但是设置了这个图片以后,运行的效果是,点击选择后,本身的image就 ...

  3. 常用AWK命令

    常用AWK命令 Awk is a programming language which allows easy manipulation of structured data and the gene ...

  4. NOIP200905

    暴力,简单,,, 题目好长,差点没读完 试题描述 R国和S国正陷入战火之中,双方都互派间谍,潜入对方内部,伺机行动.历经艰险后,潜伏于S国的R国间谍小C终于摸清了S国军用密码的编码规则:1)S国军方内 ...

  5. matlab2014在mac Yosemite下出现java空指针情况

    恢复方法为 使用xcode打开 /System/Library/CoreServices/SystemVersion.plist 将 ProductVersion 下的10.10或10.10.1改为1 ...

  6. sae-xhprof调试性能

    1. 在storage中创建xhprof的domain 2.在xhprof中,给对应的版本应用开启调试 3.在版本内的代码加入 sae_xhprof_start(); // 需要调优的代码 // .. ...

  7. 使用内核定时器的second字符设备驱动及测试代码

    驱动: #include <linux/kernel.h> #include <linux/module.h> #include <linux/fs.h> #inc ...

  8. 用Cocos2d-x实现2D光线效果

    2015.3.23优化修改,现在已经能达到稳定60帧了.. 本博客地址:http://www.cnblogs.com/wolfred7464/ 创意来自于:http://ncase.me/sight- ...

  9. SLua

    安装 1.下载最新版,将Assets目录里的所有内容复制到工程中,对于最终产品,可以删除例子,文档等内容,如果是开发阶段则无所谓. 2.等待unity编译完毕,如果一切顺利的话,将出现SLua菜单,点 ...

  10. CoreBluetooth - 中心模式

    BLE中心模式流程-coding BLE中心模式流程 - 1.建立中心角色 - 2.扫描外设(Discover Peripheral) - 3.连接外设(Connect Peripheral) - 4 ...