android获得联系人问题

package com.yarin.android.example03_01;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.PhoneLookup;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


public class Activity02 extends Activity
{
public void onCreate(Bundle savedInstanceState)
{


super.onCreate(savedInstanceState);
/* 设置显示main2.xml布局 */
setContentView(R.layout.main2);

TextView textView=(TextView) this.findViewById(R.id.textView);
String contactString="";
ContentResolver contentResolver=this.getContentResolver();
Cursor cursor=contentResolver.query(ContactsContract.Contacts.CONTENT_URI, null,null,null,null);
while(cursor.moveToNext()){
int nameFieldColumnIndex=cursor.getColumnIndex(PhoneLookup.DISPLAY_NAME);
String name=cursor.getString(nameFieldColumnIndex);
contactString+=name;
int contactid=cursor.getInt(cursor.getColumnIndex(PhoneLookup._ID));
int has_phone_number=cursor.getColumnIndex(PhoneLookup.HAS_PHONE_NUMBER);

if(has_phone_number>0){
Cursor phoneNumbers=contentResolver.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID+"="+contactid, null, null);
if(phoneNumbers.moveToFirst()){
while(phoneNumbers.moveToNext()){
int numberFieldColumnIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);//不知道为什么这里获得总是numberFieldColumnIndex=-1所以总是出错
String number=cursor.getString(numberFieldColumnIndex);
contactString+="--"+number;
}
}
phoneNumbers.close();
}

}

cursor.close();
textView.setText(contactString);


/* findViewById(R.id.button2)取得布局main.xml中的button2 */
Button button = (Button) findViewById(R.id.button2);
/* 监听button的事件信息 */
button.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v)
{
/* 新建一个Intent对象 */
Intent intent = new Intent();
/* 指定intent要启动的类 */
intent.setClass(Activity02.this, Activity01.class);
/* 启动一个新的Activity */
startActivity(intent);
/* 关闭当前的Activity */
Activity02.this.finish();
}
});
}
}

numberFieldColumnIndex=cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER);//不知道为什么这里获得总是numberFieldColumnIndex=-1所以总是出错,联系人我新建了,可是还是-1,请高手给看看

你可能感兴趣的:(android,xml,OS)