get 0 when selecting contact from contact list - android

I have made a application in which i want to user to select the contact from the contact list and display it in the EditText.But when ever my user selects a contact my edit text value shows only 0.Why this so???
Code
case R.id.bt_contacts:
intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
break;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == getActivity().RESULT_OK) {
Uri contactData = data.getData();
Cursor c = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, null, null, null);
if (c.moveToFirst()) {
contactNumber = c.getInt(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
}
etMobile.setText("" + contactNumber);
}
}

I think you should get string value from cursor
contactNumber = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));

Related

How do I open a contact selection screen with "search by default"

How can I navigate to this screen using android intent
Add Read Contacts permissions.
Use the code below.
public void showContactsChooser(final View view){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
}
We now get a screen that is showing us all the contacts we have. We choose one and we are getting back to our app.
To read this contact I am using this method:
#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 = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()){
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
Toast.makeText(getApplicationContext(), name, Toast.LENGTH_SHORT).show();
}
}
}
}
For further info use this link.
https://developer.android.com/training/contacts-provider/retrieve-names

Get phone number from Contacts - Crashes in my onActivityResult method when using ContactsContract

I created an onActivityResult method. I am in the contact list, Upon clicking I should get the name and the phone number of the contact, but it crashes.
#Override
protected void onActivityResult(int reqCode, int resultCode, Intent data) {
//TODO Auto-generated method stub
super.onActivityResult(reqCode, resultCode, data);
if (reqCode == PICK_CONTACT) {
if(resultCode == ActionBarActivity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
if(c.moveToFirst()) {
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);
String name = c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
Toast.makeText(this, "You've picked" + name, Toast.LENGTH_SHORT).show();
}
}
}
}
Please note, if I remove the line
String phone = c.getString(c.getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
it doesn't crash. (But I don't get the phone number.

How to get contact number from contactlist in Android?

I want to get contact number from contact list. In Android application on button i want get number from contact list of phone.
Means it click on Select button, & open contact list. it select number, & display in textview.
Please give me a solution.
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.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 = getContentResolver().query(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
I got this answer from the following link
http://tutorials-android.blogspot.in/2011/11/how-to-call-android-contacts-list.html

Get a phone number from contacts

I have a button that I click and that shows contacts. It displays the name, but not the number of a selected contact...
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 1);
}
});
}
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
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(ContactsContract.Contacts.DISPLAY_NAME));
contactName.setText(name);
Something here !!!!!!!!!!!!!!!!!!!!!!!!
contactName.setText(number);
}
}
}
}
It String the NAME but how can I get the phone number?
#Override
public void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext())
{
String name=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String number=phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
contactName.setText(name);
contactNumber.setText(number);
}
}
With this code it opens my contacts. I touch one, but it always display the LAST name and number that I have added to contacts.
For example, I touch Adrian (22222222) and it shows Paul (33333333) which is the last person that I have added.
What should I do?
String phoneNumber = c.getString(c.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));

Contacts from phone in own application

How can I implement contacts in my own app? I want to, when I click on button, show all contacts from my phone. Can you tell me something about it? Like in when you want send SMS. Click on button, choose contacts and send.
First you query the full contacts and add into list view and select contact, then you search of query contacts.
Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
phones.moveToFirst();
for(int i =0;i<phones.getCount();i++)
{
String lookupKey = phones.getString(phones.getColumnIndex(ContactsContract.Contacts.LOOKUP_KEY));
Uri uri = Uri.withAppendedPath(ContactsContract.Contacts.CONTENT_VCARD_URI, lookupKey);
}
you need to starActivityforResult with below intent
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
define a constant like int PICK_CONTACT = 1;
startActivityForResult(intent, PICK_CONTACT);
and should have onActivityResult method such as below.
#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.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
// TODO Whatever you want to do with the selected contact name.
}
}
break;
}
}
here is complete tutorial for this how-to-call-android-contacts-list

Categories

Resources