2010-12-05 4 views
4

모든 연락처의 이름과 전화 번호를 AutoCompleteTextView의 어댑터로로드하려고합니다. 어떻게하면 될까요? 예를 들어, "G"를 입력하면 드롭 다운 목록에 "Good, < 111111>", "Good, < 222222>"이 표시됩니다.커스텀 CursorAdapter에 DISPLAY_NAME과 NUMBER를 결합하는 방법은 무엇입니까?

api 데모를 사용하면 DISPLAY_NAME을 결과 커서에만 넣을 수 있습니다. 이름과 숫자를 하나의 커서로 결합하는 방법을 모르겠습니다. 감사! 이 API 데모에서

코드 :

ContentResolver content = getContentResolver(); 
Cursor cursor = content.query(ContactsContract.Contacts.CONTENT_URI, 
    PEOPLE_PROJECTION, null, null, null); 
ContactListAdapter adapter = new ContactListAdapter(this, cursor); 
mAutoCompleteTextView.setAdapter(adapter); 

private static class ContactListAdapter extends CursorAdapter implements Filterable { 
    private ContentResolver mCR; 

    public ContactListAdapter(Context context, Cursor c) { 
     super(context, c); 
     mCR = context.getContentResolver(); 
    } 

    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     final LayoutInflater inflater = LayoutInflater.from(context); 
     final TextView view = (TextView) inflater.inflate(
       android.R.layout.simple_dropdown_item_1line, parent, false); 
     view.setText(cursor.getString(1)); 
     return view; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     ((TextView) view).setText(cursor.getString(1)); 
    } 

    @Override 
    public String convertToString(Cursor cursor) { 
     return cursor.getString(1); 
    } 

    @Override 
    public Cursor runQueryOnBackgroundThread(CharSequence constraint) { 
     if (getFilterQueryProvider() != null) { 
      return getFilterQueryProvider().runQuery(constraint); 
     } 
     StringBuilder buffer = null; 
     String[] args = null; 
     if (constraint != null) { 
      buffer = new StringBuilder(); 
      buffer.append("UPPER("); 
      buffer.append(ContactsContract.Contacts.DISPLAY_NAME); 
      buffer.append(") GLOB ?"); 
      args = new String[] { constraint.toString().toUpperCase() + "*" }; 
     } 
     return mCR.query(ContactsContract.Contacts.CONTENT_URI, PEOPLE_PROJECTION, 
       buffer == null ? null : buffer.toString(), args, null); 
    } 
} 

private static final String[] PEOPLE_PROJECTION = new String[] { 
    ContactsContract.Contacts._ID, 
    ContactsContract.Contacts.DISPLAY_NAME, 
    ContactsContract.Contacts.HAS_PHONE_NUMBER 
}; 
+0

어떤 도움을 주시겠습니까? =) – yee

답변

0

숫자는 이미 커서의 일부가되어야합니다. 그렇게 그들을 검색하십시오.

final long phoneNumber;// = c.getColumnIndex(People.NUMBER_KEY); 
     if(Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0){ 
      id = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID)); 
      int nameColumn = cursor.getColumnIndex(People.NAME); 
      int numberKeyColumn = cursor.getColumnIndex(People.NUMBER_KEY);    
      phoneNumber = this.getPhoneNumbers(id); }   

그런 다음 동일한 클래스의 getPhoneNumbers 메소드가 다시 반복해야합니다. 커서를 (새로운 api에서) 바인딩하여 더 나은 방법이 있는지 모르겠지만 작동합니다.

 public Long getPhoneNumbers(String id) { 
     Long number = new Long(0); 
     ContentResolver cr = this.context.getContentResolver(); 
     Cursor pCur = cr.query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
       new String[]{id}, null); 
     pCur.moveToFirst(); 
     if (pCur.getCount() > 0){ 
      String num = pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
      num = num.replaceAll("-", ""); 
      number = Long.parseLong(num); 
     } 
     pCur.close(); 
     return number; 
    }