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

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

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

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

请看代码:

 1 public class MyAdapter extends BaseAdapter {
 2     
 3     private List<PhoneBean> lists;
 4     private Context context;
 5     
 6     public MyAdapter(Context context){
 7 
 8         this.lists = GetPhoneFromSQL.getPhoneInfo(context);
 9         this.context=context;
10         
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     }
21 
22     @Override
23     public int getCount() {
24         return lists.size();
25     }
26 
27     @Override
28     public Object getItem(int position) {
29         return position;
30     }
31 
32     @Override
33     public long getItemId(int position) {
34         return position;
35     }
36 
37     @Override
38     public View getView(int position, View convertView, ViewGroup parent) {
39         ViewHolder holder=null;
40         if (convertView==null) {
41             holder=new ViewHolder();
42             convertView=LayoutInflater.from(context).inflate(R.layout.phonedetails, null);
43             holder.showName=(TextView) convertView.findViewById(R.id.showName);
44             convertView.setTag(holder);
45         }else {
46             holder=(ViewHolder) convertView.getTag();
47         }
48         holder.showName.setText(lists.get(position).getName());
49         notifyDataSetChanged();
50         return convertView;
51     }
52     
53     static class ViewHolder{
54         public TextView showName;
55     }
56 
57 
58 }

红色字体为新加的广播。

保存数据的部分:

 1 public class SavePhoneToSQL {
 2     
 3     private static final String DBNAME="Phones";
 4     private static PhoneBean phoneBean;
 5     private static DbUtils db;
 6     static List<PhoneBean> lists ;
 7 
 8     public static void saveData(Context context,String name,String number,String company,String email) {
 9         phoneBean = new PhoneBean();
10         phoneBean.setName(name);
11         phoneBean.setNumber(number);
12         phoneBean.setCompany(company);
13         phoneBean.setCompany(email);
14         
15         db = DbUtils.create(context);
16         db.configAllowTransaction(true);
17         db.configDebug(true);
18         try {
19             db.createTableIfNotExist(PhoneBean.class);
20         } catch (DbException e1) {
21             Toast.makeText(context, "创建数据库失败", Toast.LENGTH_SHORT).show();
22         }
23         try {
24             db.save(phoneBean);
25             Intent intent = new Intent();
26             intent.setAction("com.lijingbo.getmyphonenumber.PHONE_SQL_CHANGED");
27             context.sendBroadcast(intent);
28         } catch (DbException e) {
29             // TODO Auto-generated catch block
30             Toast.makeText(context, "数据保存失败", Toast.LENGTH_SHORT).show();
31         }
32 
33         
34     }
35     
36     public static List<PhoneBean> getPhoneInfo() {
37         try {
38             lists=db.findAll(PhoneBean.class);
39         } catch (DbException e) {
40             // TODO Auto-generated catch block
41             e.printStackTrace();
42         }
43         return lists;
44     }
45 }
原文地址:https://www.cnblogs.com/liyiran/p/4669463.html