1. 加载会话列表分两步加载。
且第1步和第2步之间有些延迟,便于第1步的结果及时在ListView中显示出来。
mHandler.postDelayed(new LoadThreadsExtra(), 200);
第1步。从threads表中查找出数据,封装成TxrjThread,构建ConversationAdapter,在ListView中显示出来。
第2步。从sms, canonical_address, data等表中查找出hasDraftMsg, hasFailMsg, unreadCount, number, name等信息,调用notifyDataChanged()更新数据。
class LoadThreadsExtra implements Runnable {
List<Long> draftIds = null; List<Long> failIds = null; Map<Long, Integer> unreadMap = null; Map<Integer, String> canonicalAddrMap = null; Map<String, String> nameAddrMap = null;@Override
public void run() { draftIds = SmsDataManager.getThreadsWithDraftMsg(mContext); failIds = SmsDataManager.getThreadsWithFailedMsg(mContext); unreadMap = SmsDataManager.getThreadUnreadCountMap(mContext); canonicalAddrMap = SmsDataManager.getCanonicalAddressMap(mContext); nameAddrMap = SmsDataManager.getNameAddressMap(mContext); int threadCount = mThreads.size(); for(int i=0;i<threadCount;i++){ TxrjThreads thread = mThreads.get(i); long threadId = thread.getThreadId(); thread.setHasDraftMsg(draftIds.contains(threadId)); thread.setHasFailMsg(failIds.contains(threadId)); thread.setUnReadCount(unreadMap.containsKey(threadId) ? unreadMap.get(threadId) : 0); setNameNumber(thread, thread.getRecipientId()); } mHandler.sendEmptyMessage(TxrjConstant.WHAT_NOTIFY_DATA_CHANGED); } private void setNameNumber(TxrjThreads thread, String recipientIds) { // 提取Number. String[] ids = recipientIds.trim().split(" "); StringBuffer sb = new StringBuffer(); for (int i = 0; i < ids.length; i++) { sb.append(canonicalAddrMap.get(Integer.valueOf(ids[i]))); if (i != ids.length - 1) { sb.append(","); } } thread.setNumber(sb.toString()); sb = new StringBuffer(); // 提取Name. String[] numbers = thread.getNumber().split(","); for (int i = 0; i < numbers.length; i++) { String name = nameAddrMap.get(numbers[i]); sb.append(name == null ? numbers[i] : name); if (i != numbers.length - 1) { sb.append(","); } } thread.setName(sb.toString()); } } 2. 进入ConversationListActivity界面中,在加载数据之前也要延迟一点时间,便于Activity将UI及时显示出来,避免了UI显示慢的现象。mHandler.postDelayed(new Runnable() { public void run() { new LoadThreadsTask().execute(); } }, 200);