I'm having trouble getting the number of contacts selected by my contacts in my app I as a new activity made the call app do, when you select a contact, activity, gives his name, so I need to return the number of which was the user to enter the contact. I found the code in some tutorials on the internet ... Here is the code
This I say how contacted activity
mSuspectButton = (Button) v.findViewById (R.id. Crime_suspect); mSuspectButton.setOnClickListener (New View.OnClickListener () {public void onClick (View v) {Intent i = new Intent (Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); startActivityForResult (i, REQUEST_CONTACT);}});
This is How do I get selected Contact Name:
@Override public void OnActivityResult (integer requestCode, integer resultCode, intent data) {if ( ResultCode! = Activity. RESULT_OK) Return; If (requestcode == REQUEST_CONTACT) {Ury Contact Yuri = Data .getData (); // Specify which query you want to return the // values for your query. String [] queryFields = new string [] {ContactsContract.Contacts.DISPLAY_NAME}; // to your query performance - contactUri like a "where" clause // cursor on c = getActivity () getContentResolver () query (contactUri, queryFields, null, null, null); .. // Again check that you actually get results if (c.getCount () == 0) {c.close (); Return; } // Remove the first column of the first row of data - // which is the name of your suspect. C.moveToFirst (); String Suspicious = c.getString (0); MCrime.setSuspect (Suspicious); MSuspectButton.setText (Suspicious); C.close (); }}
So no one can help, I really do not understand how it works.
Use this in your onActivityResult, this will return the number you selected
< Pre> @Override Public Zero onActivityResult (integer reqCode, integer resultCode, intent data) {super.onActivityResult (record, resultcode, data); Switch (case) {case (PICK_CONTACT): if (resultCode == activity. RESULT_OK) {URI contactData = data.getData (); Cursor c = managed query (contact data, empty, null, empty, zero); If (c.moveToFirst ()) {string id = c.getString (c.getColumnIndexOrThrow (contactContact.Contacts_ID)); String isphone = c. Getstring (c.getColumnIndex (contactsContract.Contacts.HAS_PHONE_NUMBER)); If (hasPhone.equalsIgnoreCase ( "1")) {cursor phone = getContentResolver () query (ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, ContactsContract.CommonDataKinds.Phone.CONTACT_ID + "=" + id, null, null). Phones.moveToFirst (); String cNumber = phones.getString (phones.getColumnIndex (ContactsContract.CommonDataKinds.Phone.NUMBER)); }}}}}
No comments:
Post a Comment