我写了一些代码来获取联系人的电话号码和用户名,但我发现我的代码中存在一些问题,用户在文本视图中显示但是号码没有显示,请求帮助我 - 我的代码如下 - < / p>
public class MainActivity extends Activity {
public static final int PICK_CONTACT = 1;
private Button btnContacts;
private TextView txtContacts1;
private TextView txtContacts2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts1 = (TextView) findViewById(R.id.txt_contacts_name);
txtContacts2 = (TextView) findViewById(R.id.txt_contacts_number);
btnContacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK,
People.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name =c.getString(c.getColumnIndexOrThrow(People.NAME))+" : "+c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
txtContacts1.setText(name);
}
}
break;
}
}
}
谢谢..
答案 0 :(得分:0)
People
api用于访问已弃用的联系人。您应该使用ContactsContract
。
我修改了你的代码,现在工作正常。这里是。
public class MainActivity extends Activity {
public static final int PICK_CONTACT = 1;
private Button btnContacts;
private TextView txtContacts1;
private TextView txtContacts2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnContacts = (Button) findViewById(R.id.btn_contacts);
txtContacts1 = (TextView) findViewById(R.id.txt_contacts_name);
btnContacts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType(ContactsContract.Contacts.CONTENT_TYPE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, PICK_CONTACT);
}
}
});
}
@Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
String cNumber = null;
switch (reqCode) {
case (PICK_CONTACT):
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String id = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));
String hasPhone = c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));
if (hasPhone.equalsIgnoreCase("1")) {
Cursor phones = getContentResolver().query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = " + id,
null, null);
phones.moveToFirst();
cNumber = phones.getString(phones.getColumnIndex("data1"));
//System.out.println("number is:" + cNumber);
}
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
txtContacts1.setText(name + " : " + cNumber);
}
}
break;
}
}
}