关于android通讯录加载大数据的优化问题

在最近的开发中,遇到一个问题,在自己开发的通讯录中,500条联系人加载时慢的问题。 
最初把同步本地通讯录的操作,放在软件的loading页去做,把联系人读到缓存中,发现当数据大时,loading页会进入得很慢。然后试着用CursorAdapter去做,实现列表滚动去读数据库,但又发现当用户平凡刷列表时,会出现内存溢出的情况。 

那怎么办呢,于是我综合两种情况的优点,进行了合并。当用户在滑动列表时,会把列表显示的部分通过读数据。读出来的数据放入一个MAP中,那么当用户下一次滑到之前的位置时,只需要从MAP中读取数据了。这样即解决了数据量大量,加载到缓存慢,又解决了滑动列表卡的问题。下面是代码 

1.首先通过android 通讯录数据库提供的索引表ContactsContract.RawContacts.CONTENT_URI把所有的联系人ID查询出来。 

Java代码   收藏代码
  1. private List<Long> loadAllContactIds(){  
  2.     List<Long> arr = new ArrayList<Long>();  
  3.     Cursor cursor = cr.query(ContactsContract.RawContacts.CONTENT_URI, null, ContactsContract.RawContacts.DELETED + " = 0",null,null);  
  4.     if(null != cursor && cursor.moveToFirst()){  
  5.         do{  
  6.             long id = cursor.getLong(cursor.getColumnIndex(ContactsContract.RawContacts.CONTACT_ID));  
  7.             arr.add(id);  
  8.         }while(cursor.moveToNext());  
  9.         cursor.close();  
  10.     }  
  11.     return arr;  
  12.    }  



2.再通过ID把联系人的各种信息读出来。 

Java代码   收藏代码
  1. private String loadAllContactInfo(long id){  
  2.         Cursor cursor = cr.query(ContactsContract.Data.CONTENT_URI, null, ContactsContract.Data.RAW_CONTACT_ID + " = "+id,null,null);  
  3.         StringBuffer sb = new StringBuffer();  
  4.         if(null != cursor && cursor.moveToFirst()){  
  5.             do{  
  6.                 String type = cursor.getString(cursor.getColumnIndex(ContactsContract.Data.MIMETYPE));  
  7.                 if(type.equals(StructuredName.CONTENT_ITEM_TYPE)){  
  8.                     sb.append(cursor.getString(cursor.getColumnIndex(StructuredName.DISPLAY_NAME))).append("|");  
  9.                 }else if(type.equals(Nickname.CONTENT_ITEM_TYPE)){  
  10.                     sb.append(cursor.getString(cursor.getColumnIndex(Nickname.NAME))).append("|");  
  11.                 }else if(type.equals(Phone.CONTENT_ITEM_TYPE)){  
  12.                     sb.append(cursor.getString(cursor.getColumnIndex(Phone.NUMBER))).append("|");  
  13.                 }else if(type.equals(Event.CONTENT_ITEM_TYPE) && Event.TYPE.equals(Event.TYPE_BIRTHDAY)){  
  14.                     sb.append(cursor.getString(cursor.getColumnIndex(Event.DATA1))).append("|");  
  15.                 }  
  16.             }while(cursor.moveToNext());  
  17.             cursor.close();  
  18.         }  
  19.         return sb.toString();  
  20.     }     



3.自定义的adapter , 

Java代码   收藏代码
  1. private class MyAdapter extends BaseAdapter{  
  2.       
  3.     private List<Long> ids;  
  4.     private Map<Long,String> maps;    
  5.       
  6.     public MyAdapter(List<Long> ids){  
  7.         this.ids = ids;  
  8.         maps = new HashMap<Long, String>();  
  9.     }  
  10.   
  11. @Override  
  12. public int getCount() {  
  13.    return this.ids.size();  
  14. }  
  15.   
  16. @Override  
  17. public Object getItem(int position) {  
  18.    return this.ids.get(position);  
  19. }  
  20.   
  21. @Override  
  22. public long getItemId(int position) {  
  23.    return position;  
  24. }  
  25.   
  26. @Override  
  27. public View getView(int position, View convertView, ViewGroup parent) {  
  28.             if(null == convertView){  
  29.     convertView = LayoutInflater.from(TextTextActivity.this).inflate(android.R.layout.simple_list_item_1, null);  
  30.     }  
  31.     long id = ids.get(position);  
  32.     if(convertView instanceof TextView){  
  33.                     String res = maps.get(id);  
  34.         TextView tv = (TextView)convertView;  
  35.         if(TextUtils.isEmpty(res)){  
  36.             res = loadAllContactInfo(id);  
  37.             maps.put(id, res);  
  38.         }  
  39.         tv.setText(res);  
  40.     }  
  41.     return convertView;  
  42. }  
  43.    }   

猜你喜欢

转载自coolwhy1.iteye.com/blog/1895056
今日推荐