2014-03-14 5 views
3

Android 전화 번호부 양식 연락처를 받으려합니다. 나는 많은 방법을 시도했지만 나는 전화 번호를 얻을 수 없다. 연락처 이름과 상태가 있는데 전화 번호를 얻으려고하면 작동하지 않습니다. 내 코드는입니다.Android 연락처에서 전화 번호 받기

public class ContactsAdapter extends GroupingCursorAdapter { 
    public static final String[] PROJECTION = new String[] { 
     Phone._ID, 
     Phone.LOOKUP_KEY, 
     Phone.DISPLAY_NAME, 
     Phone.CONTACT_STATUS, 
     Phone.TIMES_CONTACTED, 
     Phone.LAST_TIME_CONTACTED, 
     Phone.STARRED, 
     Phone.NUMBER 

    }; 
    public static final int COLUMN_PHONE_ID = 0; 
    public static final int COLUMN_LOOKUP_KEY = 1; 
    public static final int COLUMN_DISPLAY_NAME = 2; 
    public static final int COLUMN_CONTACT_STATUS = 3; 
    public static final int COLUMN_TIMES_CONTACTED = 4; 
    public static final int COLUMN_LAST_TIME_CONTACTED = 5; 
    public static final int COLUMN_STARRED = 6; 
    public static final int COLUMN_NUMER=7; 

    private class ViewCache { 
     public final TextView contactName; 
     public final TextView contactLastDialed; 
     public final TextView contactInformation; 
     public final TextView contactCallCount; 
     public final ImageView contactImage; 
     public final ImageView contactStarred; 

     public ViewCache(View base) { 
      contactName = (TextView)base.findViewById(R.id.ContactName); 
      contactLastDialed = (TextView)base.findViewById(R.id.ContactLastDialed); 
      contactInformation = (TextView)base.findViewById(R.id.ContactInformation); 
      contactCallCount = (TextView)base.findViewById(R.id.ContactCallCount); 
      contactImage = (ImageView)base.findViewById(R.id.ContactImage); 
      contactStarred = (ImageView)base.findViewById(R.id.ContactStarred); 
     } 

    } 


    private Resources mResources; 
    private AsyncContactImageLoader mAsyncContactImageLoader; 
    private boolean mShowCallCounter; 

    public ContactsAdapter(Context context, Cursor cursor, AsyncContactImageLoader asyncContactImageLoader) { 
     super(context, cursor, Phone.LOOKUP_KEY); 
     mResources = context.getResources(); 
     mAsyncContactImageLoader = asyncContactImageLoader; 
     mShowCallCounter = false; 
    } 


    @Override 
    public View newView(Context context, Cursor cursor, ViewGroup parent) { 
     if (shouldBeGrouped(cursor)) { // we just hide the grouped views 
      View view = new View(context); 
      view.setLayoutParams(new AbsListView.LayoutParams(0, 0)); 
      return view; 
     } 
     View view = LayoutInflater.from(context).inflate(R.layout.contact_item, null); 
     ViewCache viewCache = new ViewCache(view); 
     view.setTag(viewCache); 

     return view; 
    } 

    @Override 
    public void bindView(View view, Context context, Cursor cursor) { 
     final ViewCache viewCache = (ViewCache)view.getTag(); 
     if (viewCache == null) { // empty view has no viewcache, and we do nothing with it 
      return; 
     } 
     String displayName = cursor.getString(COLUMN_DISPLAY_NAME); 
     if (displayName == null) { 
      displayName = ""; 
     } 

     viewCache.contactName.setText(displayName); 

     long lastTimeContacted = cursor.getLong(COLUMN_LAST_TIME_CONTACTED); 
     if (lastTimeContacted == 0) { 
      viewCache.contactLastDialed.setText(mResources.getString(R.string.not_contacted)); 
     } else { 
      viewCache.contactLastDialed.setText(DateUtils.getRelativeTimeSpanString(lastTimeContacted)); 
     }  
savedInstanceState; 
     String number= cursor.getString(COLUMN_NUMBER); 
     if (status == null) { 
      number= "03324004738 Dummy"; 
     } 

     status = status.replace('\n', ' '); 
     viewCache.contactInformation.setText(status); 

     int timesContacted = cursor.getInt(COLUMN_TIMES_CONTACTED); 
     if (mShowCallCounter) { 
      viewCache.contactCallCount.setText("(" + timesContacted + ")"); 
     } else { 
      viewCache.contactCallCount.setText(""); 
     } 

     int starred = cursor.getInt(COLUMN_STARRED); 
     viewCache.contactStarred.setVisibility(starred == 1? View.VISIBLE : View.GONE); 

     String lookupKey = cursor.getString(COLUMN_LOOKUP_KEY); 
     if (lookupKey == null) { // should absolutely never happen 
      lookupKey = ""; 
     } 
     viewCache.contactImage.setTag(lookupKey); // set a tag for the callback to be able to check, so we don't set the contact image of a reused view 
     Drawable d = mAsyncContactImageLoader.loadDrawableForContact(lookupKey, new ImageCallback() { 

      @Override 
      public void imageLoaded(Drawable imageDrawable, String lookupKey) { 
       if (lookupKey.equals(viewCache.contactImage.getTag())) { 
        viewCache.contactImage.setImageDrawable(imageDrawable); 
       } 
      } 
     }); 
     viewCache.contactImage.setImageDrawable(d); 
    } 

    public void setShowCallCounter(boolean showCallCounter) { 
     mShowCallCounter = showCallCounter; 
    } 
} 
+0

나는 또한 Number of Insted of String을 시도했다. –

답변

1

당신이 올바른 접촉 lookupkey를 얻을 수 있는지 확인합니다. 여기 내 코드가있다. 테스트 할 수 있습니다.

private static final String[] PHONE_PROJECTION = { Phone.NUMBER, }; 
private static final String PHONE_LOOKUP_KEY_SELECTION = Data.LOOKUP_KEY 
      + "=?" + " AND " + Data.MIMETYPE + "='" + Phone.CONTENT_ITEM_TYPE 
      + "'"; 
public String getPhoneNumber(String lookupKey) { 
     String result = null; 
     Cursor cursor = null; 
     try { 
      cursor = mContext.getContentResolver().query(Data.CONTENT_URI, 
        PHONE_PROJECTION, PHONE_LOOKUP_KEY_SELECTION, 
        new String[] { lookupKey }, null); 
      if (cursor != null && cursor.moveToFirst()) { 
       result = cursor.getString(0); 
      } 
     } catch (Exception e) { 

     } finally { 
      if (cursor != null) { 
       cursor.close(); 
      } 
     } 
     return result; 
    } 
관련 문제