简体   繁体   中英

How to get phone number from a specific contact in Android?

Alright, so the book I'm going through wants me to call a specific person.

I can retrieve the contact just fine, it's the whole "getting contact ID to find the phone number bit" that I'm having trouble with.

The app is crashing every time I select a contact.

I suppose I'm not sure how to properly navigate to the contact ID.

I am fairly new to Android and now I'm just getting completely lost, I haven't be able to find a solution on here that helps me.

Relevant Code:

   }else if(requestCode == REQUEST_CONTACT) {
        Uri contactsURI = data.getData();

        String[] queryFields = new String[]{
                ContactsContract.Contacts.DISPLAY_NAME
        };


        //Perform your query - the contactURI is like a "where"
        //clause here
        Cursor c = getActivity().getContentResolver().query(contactsURI, queryFields, null, null, null);


        //Double-check that you actually got results
        if (c.getCount() == 0) {
            c.close();
            return;
        }
        contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

        //Pull out the first column of the first row of data
        //that is your suspects name
        c.moveToFirst();

        String suspect = c.getString(0);


        mCrime.setmSuspect(suspect);
        mSuspectButton.setText(suspect);
        c.close();

    }



}

I am crashing because of this part of the above code:

contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));

You have to first call

c.moveToFirst();

Then

contactID = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM