2010-12-17 207 views
0

我試過下面的代碼,它會顯示「名稱」,但不顯示「數字」。
有人能指出我犯了什麼錯誤嗎?沒有得到android的電話號碼

package org.testcont; 

import android.app.Activity; 
import android.content.ContentResolver; 
import android.content.ContentUris; 
import android.database.Cursor; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.Contacts; 
import android.provider.Contacts.People; 
import android.widget.EditText; 
import android.widget.TextView; 

public class testcontActivity extends Activity { 
    /** Called when the activity is first created. */ 
    TextView tvname; 
    TextView tvnumber; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     tvname = (TextView) findViewById(R.id.TextView01); 
     tvnumber = (TextView) findViewById(R.id.TextView02); 

     Cursor c=getContentResolver().query(People.CONTENT_URI,null,null,null,null); 
     while(c.moveToNext()){ 
     int name=c.getColumnIndexOrThrow(People.NAME); 
     String nm=c.getString(name); 
     tvname.setText(nm); 
     int number=c.getColumnIndexOrThrow(People.NUMBER); 
     String s=c.getString(number); 

     } 
     c.close(); 
    } 
} 

回答

0

您正在使用舊的API,而你應該使用這個

android.provider.ContactsContract.CommonDataKinds.Phone 

類檢索聯繫人姓名和電話號碼也做如下修改:

Cursor c=getContentResolver().query(Phone.CONTENT_URI,null,null,null,null); 
int name=c.getColumnIndexOrThrow(Phone.DISPLAY_NAME); 
int number=c.getColumnIndexOrThrow(Phone.NUMBER);