Android开发之使用BaseAdapter的notifyDataSetChanged()无法更新列表

在做一个通讯录的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开发 - 下拉刷新和分段头悬停列表


下一篇:IOS贝塞尔曲线圆形进度条和加载动画