2014-09-22 2 views
0

전화상의 모든 연락처의 이름과 전화 번호로 목록보기를 만들어야합니다. 코드가 작동하지만 사용자가 많은 연락처를 가지고있을 때 너무 많은 시간이 걸립니다. 개선 할 수있는 방법이 있습니까?개선 시간 전화 번호 받기

public List<Contact> fetchContacts() { 

    String phoneNumber = null; 
    String email = null; 

    Uri CONTENT_URI = ContactsContract.Contacts.CONTENT_URI; 
    String _ID = ContactsContract.Contacts._ID; 
    String DISPLAY_NAME = ContactsContract.Contacts.DISPLAY_NAME; 
    String HAS_PHONE_NUMBER = ContactsContract.Contacts.HAS_PHONE_NUMBER; 

    Uri PhoneCONTENT_URI = ContactsContract.CommonDataKinds.Phone.CONTENT_URI; 
    String Phone_CONTACT_ID = ContactsContract.CommonDataKinds.Phone.CONTACT_ID; 
    String NUMBER = ContactsContract.CommonDataKinds.Phone.NUMBER; 
    ContentResolver contentResolver = getContentResolver(); 

    Cursor cursor = getContentResolver().query(CONTENT_URI, null, null, null, DISPLAY_NAME + " ASC"); 

    List<Contact> contactList = new ArrayList<Contact>(); 

    // Loop for every contact in the phone 
    if (cursor.getCount() > 0) { 
     while (cursor.moveToNext()) { 

      String contact_id = cursor.getString(cursor.getColumnIndex(_ID)); 
      String name = cursor.getString(cursor.getColumnIndex(DISPLAY_NAME)); 
      int hasPhoneNumber = Integer.parseInt(cursor.getString(cursor.getColumnIndex(HAS_PHONE_NUMBER))); 
      if (hasPhoneNumber > 0) { 
       Cursor phoneCursor = contentResolver.query(PhoneCONTENT_URI, null, Phone_CONTACT_ID + " = ?", new String[]{contact_id}, null); 
       phoneCursor.moveToNext(); 
       phoneNumber = phoneCursor.getString(phoneCursor.getColumnIndex(NUMBER)); 
       phoneCursor.close(); 
       Contact contacto = new Contact(); 
       contacto.name = name; 
       contacto.phone = phoneNumber; 
       contacto.id = contact_id; 
       contactList.add(contacto); 
      } 
     } 
    } 

    return contactList; 
} 
+0

이 질문의 제목에 Google 검색을 넣지 마십시오 목록보기에 재활용 빨리보기 위해 ViewHolder 패턴을 찾아 넣어 :에서 newView .findViewById (...)

팁이 모두 수행해야합니다 * 당신이 기대하는 실제 검색 결과 귀하의 질문 제목에 ur Google 검색 *. –

답변

1

휴대 전화의 모든 연락처를 한 번에 가져옵니다. 이것이 시간이 걸리는 이유입니다. 당신이 대신해야 할 것은 CursorAdapter 사용할 수 있습니다 : http://developer.android.com/reference/android/widget/CursorAdapter.html

당신이 정의를 (선택)에서 (투사) 및 목록 항목보기 레이아웃에 : 당신이에 어댑터를 설정보다

public SimpleCursorAdapter(android.content.Context context, int layout, android.database.Cursor c, java.lang.String[] from, int[] to, int flags) { /* compiled code */ } 

어댑터가 실제로 한 번에 모든 연락처 만 (일부 추가와) 눈에 보이는 전망을 위해, 덩어리에서 연락처를 가져 오는 것이 아니라, 무엇을

mListView.setAdapter(mAdapter); 

:에있는 ListView.

또 다른 팁 : 당신은 구현해야합니다

@Override 
public View newView(Context context, Cursor cursor, ViewGroup parent) { 
... 
} 

@Override 
public void bindView(View view, Context context, Cursor cursor) { 
    ... 
} 

의 Bindview 만 newView 새로운 뷰를 생성하고 기본적으로 (의 Bindview에 빠른 액세스를 위해 당신을 내부 뷰에 대한 포인터를 설정 커서에서 나오는 데이터를 결합한다 ..

관련 문제