android - Getting the correct contact name by phone number -
I'm trying to get the correct contact name from a phone number so that it's ListView
.
But I'm getting the same contact name for all the phone numbers every time ...
Here's my code:
Private string GetContactName (string string) {string name = zero; ContentResolver cr = getContentResolver (); Cursor cur = cr.query (ContactsContract.Contacts.CONTENT_URI, faucet, empty, empty, empty); If (cur.getCount ()> gt; {} (while cur.moveToNext ()) {string id = cur.getString (cur.getColumnIndex (contactsContract.Contacts.HAS_PHONE_NUMBER)); Name = cur.getString (cur.getColumnIndex (ContactsContract.Contacts.DISPLAY_NAME)); }} Name of return; } Click Public Zero (see V) {ContentResolver contentResolver = getContentResolver (); Cursor cursor = contentResolver.query (Uri.parse ("content: // sms / inbox"), blank, empty, null, empty); Int indexBody = cursor.getColumnIndex (SmsReceiver.BODY); Int IndexADDR = Cursor. Item Column Index (SMSRECover.ADADARA); // ipxandr = integer PRII (getContactName (integer trostring (cursor.Test column index) (SmsReceiver.ADDRESS)))); //getContactName(Integer.toString (indexAddr)); //// If (IndexBody & lt; 0 || cursor.movtefst ()) returns; SmsList.clear (); Do {// string str = "sender:" + cursor.getstrings (indexandr) + "\ n" + cursor.get string (indexbodys); String str = "sender:" + getContactName (cursor .getString (indexAddr)) + "\ n" + cursor.get string (indexbox); SmsList.add (STR); } While (cursor.movetoonext ()); ListView smsListView = (ListView) findViewById (R.ID.SMSList); RegisterForContextMenu (smsListView); SmsListView.setAdapter (new ArrayApplator (this, Android .R.Let.simple_l_l_item, SMSList); SmsListView.setOnItemClickListener (this); }
I do not know where my error is ... and get the same name for all sender numbers.
Thank you
This is the right code for your function.
Private string getContactName (string phone number) {string name = faucet; ContentResolver cr = getContentResolver (); Cursor cur = cr.query (Uri.withAppendedPath (PhoneLookup.CONTENT_FILTER_URI, Uri.encode (phoneNumber)), new string [] {PhoneLookup.DISPLAY_NAME}, blank, empty, blank); If (cur! = Null) {try (if (cur.getCount ()> 0) {if (cur.moveToFirst ()) {name = cur.getString (cur.getColumnIndex (PhoneLookup.DISPLAY_NAME))}} } Finally {cur.close ();}} name of return;}
Comments
Post a Comment