在做一個通訊錄的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 }