我尝试了以下代码,它将显示“名称”,但不显示“数字” 有人可以说明我犯了错误吗?
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 :(得分: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);