2010-06-11 7 views
0

먼저 내 영어를 유감스럽게 생각합니다 ...안드로이드 전화에서 전화 번호를 받으십시오

연락처에서 전화 번호를받는 데 문제가 있습니다. 내 코드 접촉 전화 번호가없는

import android.app.ListActivity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.provider.ContactsContract; 
import android.widget.SimpleAdapter; 
import android.widget.Toast; 
import java.util.ArrayList; 
import java.util.HashMap; 


public class TestContacts extends ListActivity { 


private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>(); 

private SimpleAdapter numbers; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
    setContentView(R.layout.contacts); 
    numbers = new SimpleAdapter( 
    this, 
list, 
R.layout.main_item_two_line_row, 
new String[] { "line1","line2" }, 
new int[] { R.id.text1, R.id.text2 } ); 
    setListAdapter(numbers); 



    Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI, 
    null, null, null, null); 
    while (cursor.moveToNext()) { 
    String contactId = cursor.getString(cursor.getColumnIndex( 
    ContactsContract.Contacts._ID)); 
    String hasPhone = cursor.getString(cursor.getColumnIndex( 
    ContactsContract.Contacts.HAS_PHONE_NUMBER)); 

        //check if the contact has a phone number 
    if (Boolean.parseBoolean(hasPhone)) { 

Cursor phones = getContentResolver().query( 
    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, 
    null, 
    ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId, 
    null, null); 
while (phones.moveToNext()) { 
// Get the phone number!? 

String contactName = phones.getString( 
    phones.getColumnIndex( 
    ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME)); 

String phoneNumber = phones.getString( 
    phones.getColumnIndex( 
    ContactsContract.CommonDataKinds.Phone.NUMBER)); 
Toast.makeText(this, phoneNumber, Toast.LENGTH_LONG).show();  

drawContact(contactName, phoneNumber); 

} 
phones.close(); 
    } 
    }cursor.close(); 
} 

private void drawContact(String name, String number){ 

    HashMap<String,String> item = new HashMap<String,String>(); 
    item.put("line1",name); 
    item.put("line2",number); 
    list.add(item); 
    numbers.notifyDataSetChanged(); 

} 

} 

It'seems있어

은 (내가 에뮬레이터에 2 연락처를 추가했고 난 내 HTC 욕망에 또한 시도했다). 문제는 if (Boolean.parseBoolean (hasPhone)) 이 항상 false를 반환한다는 것입니다. 어떻게 전화 번호를 올바르게 얻을 수 있습니까?

전화 번호를 쿼리하지 않고 if 문 앞에 drawContact (String name, String number)를 호출하려했으나 작동했습니다 (이름을 두 번 그립니다). 하지만 LinearLayout에서는 사전 순으로 정렬되지 않습니다 ... 어떻게 알파벳 순으로 정렬 할 수 있습니까 (원래 연락처 응용 프로그램과 유사)?

는 루카

답변

1

확인 parseBoolean에 대한 documentation(), 조언에 감사합니다. 결과를 부울 대신 int로 구문 분석하려고 할 수 있습니다.

관련 문제