我正在尝试从联系人列表中获取姓名和号码,并以文本形式显示它们。代码的问题是,首先它不显示我的联系人列表中的所有联系人,当我只尝试在文本视图中设置名称然后它完全正常工作但是当我尝试在其他文本视图中添加数字时,应用程序崩溃。
以下是代码:
package com.example.hasnainabidkz.getcontactnum;
import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Contacts.People;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
public static final int PICK_CONTACT = 1;
private Button btnContacts;
private TextView txtContacts1;
private TextView txtContacts2;
@Override
public 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 OnClickListener() {
@Override
public void onClick(View v) {
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));
int number=c.getInt(c.getColumnIndexOrThrow(People.NUMBER));
txtContacts1.setText(name);
txtContacts2.setText(number);
}
}
break;
}
}
}
答案 0 :(得分:0)
这是我编写的一个简单代码,用于获取联系人姓名和号码
public class MainActivity extends AppCompatActivity {
EditText etFirstname, etLastname, etContact;
ImageView ivContact;
String contactID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etFirstname = (EditText) findViewById(R.id.etFirstname);
etLastname = (EditText) findViewById(R.id.etLastname);
etContact = (EditText) findViewById(R.id.etContact);
ivContact = (ImageView) findViewById(R.id.ivContact);
ivContact.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent it= new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(it, 101);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK) {
if(requestCode == 101) {
retrieveContactName(data.getData());
retrieveContactNumber(data.getData());
}
}
}
private void retrieveContactNumber(Uri uriContact) {
String contactNumber = null;
// getting contacts ID
Cursor cursorID = getContentResolver().query(uriContact,
new String[]{ContactsContract.Contacts._ID},
null, null, null);
if (cursorID.moveToFirst()) {
contactID = cursorID.getString(cursorID.getColumnIndex(ContactsContract.Contacts._ID));
}
cursorID.close();
Log.d("TAG", "Contact ID: " + contactID);
// Using the contact ID now we will get contact phone number
Cursor cursorPhone = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
new String[]{ContactsContract.CommonDataKinds.Phone.NUMBER},
ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ? AND " +
ContactsContract.CommonDataKinds.Phone.TYPE + " = " +
ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE,
new String[]{contactID},
null);
if (cursorPhone.moveToFirst()) {
contactNumber = cursorPhone.getString(cursorPhone.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
etContact.setText(contactNumber);
}
cursorPhone.close();
Log.d("TAG", "Contact Phone Number: " + contactNumber);
}
private void retrieveContactName(Uri uriContact) {
String contactName = null;
// querying contact data store
Cursor cursor = getContentResolver().query(uriContact, null, null, null, null);
if (cursor.moveToFirst()) {
// DISPLAY_NAME = The display name for the contact.
// HAS_PHONE_NUMBER = An indicator of whether this contact has at least one phone number.
contactName = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
if(contactName.contains(" ")) {
etFirstname.setText(contactName.substring(0, contactName.indexOf(" ")));
etLastname.setText(contactName.substring(contactName.indexOf(" "), contactName.length()));
} else {
etFirstname.setText(contactName);
}
}
cursor.close();
Log.d("TAG", "Contact Name: " + contactName);
}
您可以在我的github帐户中找到以下网址的完整项目 https://github.com/arshadkazmi42/Pick-Contact-from-Contacts-List-Android