2013-10-08 2 views
0

저는 Contacts 앱이 있고 ContentObserver로 로컬 ORMLite 데이터베이스를 업데이트해야합니다. 주소록 업데이트를 확인해야합니다. 그렇다면 내 로컬 데이터베이스에 저장되지 않은 연락처 이름을 업데이트해야합니다.ContentObserver가 유창한 ORMLite 데이터베이스를 만듭니다

두 개의 항목 - CallItem (외부 연락처 필드가 있음)과 ContactItem이 있습니다.

내 ContentObserver :

class CallsContentObserver extends ContentObserver { 
    Context _context; 
    Handler _handler; 
    public CallsContentObserver(Handler handler, Context context) { 
     super(handler); 
     this._context = context; 
     this._handler = handler; 

    } 

    @Override 
    public boolean deliverSelfNotifications() { 
     return true; 
    } 

    @Override 
    public void onChange(boolean selfChange) { 
     super.onChange(selfChange); 


      ArrayList<CallItem> contactsList = null; 
      DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
      Cursor contactLookupCursor = null; 
      try{ 
       Dao<CallItem,Integer> daoSubject = db.getCallDao(); 
       contactsList = (ArrayList<CallItem>) daoSubject.queryForAll(); 
       for (CallItem contact : contactsList) 
        { 

         if (contact.getCall_contact() == null) 
         { 
          String contactNumber = Uri.encode(contact.getNumber()); 
          String new_name = null; 
          contactLookupCursor =getContentResolver().query(
            Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber), 
            new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null); 
           while(contactLookupCursor.moveToNext()){ 
            new_name = contactLookupCursor.getString(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup.DISPLAY_NAME)); 
            } 
           contactLookupCursor.close(); 
           contact.setName(new_name); 
           daoSubject.update(contact); 
         } 

        } 
      } 
      catch(Exception e) 
      {e.printStackTrace();} 
      finally{db.close();} 

     } 
    } 

그래서 내가 가진 모든 내 CallItems. 그런 다음 모든 CallItem에 ContactItem이 포함되어 있는지 확인합니다. 그렇지 않은 경우 해당 번호로 새 이름을 얻고이 CallItem을 DAO로 새로운 값으로 업데이트합니다. 하지만이 솔루션은 매우 느리므로 더 유창하게 만들고 싶습니다. 어떻게해야합니까?

+0

당신은이 친구에게 어떤 해결책을 생각해 냈습니까? – Gray

+1

내 솔루션을 추가했습니다 .. –

답변

0

이 문제에 대한 해결책을 찾았습니다. 사용자가 응용 프로그램에 처음 나타날 때 모든 통화 로그를 구문 분석하지 않습니다. 처음 20 번만 구문 분석 한 다음 onScroll을 사용하여 더 많은 것을 다운로드합니다. 그래서 진행하는 이름과 로컬 데이터베이스와 동기화하는 데는 많은 시간이 걸리지 않습니다.

ORMLite는 꽤 좋지만,이 경우 ContentResolver를 사용하는 것이 더 좋습니다.

+0

흥미 롭습니다. ORMLite의'ContentResolver'에 도움이되는 기본 클래스인지 알려주십시오. – Gray

관련 문제