2013-06-01 6 views
1

로컬 연락처에서 전화 번호를 가져오고 싶지만 잘못된 것이 있습니다. 예를 들어 사람 A를 선택하면 사람 B가 표시됩니다. 다음은 코드입니다.전화 번호가 "ContactsContract"를 사용하여 올바르지 않습니다.

//the button_click 
public void testM(View v) { 
    Intent intent = new Intent(Intent.ACTION_PICK, 
      ContactsContract.CommonDataKinds.Phone.CONTENT_URI); 
    MainActivity.this.startActivityForResult(intent, 1); 
} 

// 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    // TODO Auto-generated method stub 

    super.onActivityResult(requestCode, resultCode, data); 
    final EditText phoneText = (EditText) findViewById(R.id.editText1); 
    switch (requestCode) { 

    case (1): { 
     if (resultCode == Activity.RESULT_OK) { 
      Uri contactData = data.getData(); 
      Cursor c = managedQuery(contactData, null, null, null, null); 
      c.moveToFirst(); 
      String phoneNum = this.getContactPhone(c); 
      phoneText.setText(phoneNum); 
     } 
     break; 

    } 

    } 
} 

// get the number 
private String getContactPhone(Cursor cursor) { 

    int phoneColumn = cursor 
      .getColumnIndex(ContactsContract.CommonDataKinds.Phone.HAS_PHONE_NUMBER); 
    int phoneNum = cursor.getInt(phoneColumn); 
    String phoneResult = ""; 
    // System.out.print(phoneNum); 
    if (phoneNum > 0) { 
     // get the id 
     int idColumn = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone._ID); 
     String contactId = cursor.getString(idColumn); 
     // get cursor; 
     Cursor phones = getContentResolver().query(
       ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
       null, 
       ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " 
         + contactId, null, null); 
     // int phoneCount = phones.getCount(); 
     // allPhoneNum = new ArrayList<String>(phoneCount); 
     if (phones.moveToFirst()) { 
      // traverse all the phone number 
      for (; !phones.isAfterLast(); phones.moveToNext()) { 
       int index = phones 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER); 
       int typeindex = phones 
         .getColumnIndex(ContactsContract.CommonDataKinds.Phone.TYPE); 
       int phone_type = phones.getInt(typeindex); 
       String phoneNumber = phones.getString(index); 
       switch (phone_type) { 
       case 2: 
        phoneResult = phoneNumber; 
        break; 
       } 
       // allPhoneNum.add(phoneNumber); 
      } 
      if (!phones.isClosed()) { 
       phones.close(); 
      } 
     } 
    } 
    return phoneResult; 
} 

나는이 클래스에 익숙하지 않은해야 뭔가 잘못 with'ContactsContract.CommonDataKinds'.I'm가 알고있다.

답변

1
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

super.onActivityResult(requestCode, resultCode, data); 
final EditText phoneText = (EditText) findViewById(R.id.editText1); 
switch (requestCode) { 

case (1): { 
    if (resultCode == Activity.RESULT_OK) { 
     Uri contactData = data.getData(); 
     Cursor c = getContentResolver().query(contactData, new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER}, null, null, null); 
     if (c.moveToFirst()) 
     { 
      int columnIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER) 
      String phoneNum = c.getString(columnIndex); 
      phoneText.setText(phoneNum); 
     } 
    } 
    break; 

} 

} 

}

+0

이 세상에 오! 다시 당신 뿐이야! 당신은 놀라운! 고마워요! 그건 그렇고, 내게 자바를 배우기에 좋은 웹 사이트를 추천 해 주시겠습니까? –

+0

좋은 웹 사이트를 알고 싶습니다. 나는 결코 어떤 웹 사이트 또는 책에서 배울 수 없다, 나는 나가 필요로하는 다만 google이다. 예를 들어 자바에서 문자열을 정수로 변환하는 방법을 모른다면 "자바로 문자열을 Java로 변환하는 방법"을 google로 그냥 입력하십시오. –

관련 문제