get Phone numbers

Put your problem here if it does not fit any of the other categories.

get Phone numbers

Postby PolarUrso » Fri Jun 11, 2010 10:49 am

Hi!
First of all i'm sorry for my english... :roll:

I've a problem getting phone numbers from contacts.

That's my code

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package ch.usi.inf.ursol.unisms;
  2.  
  3. import android.app.ListActivity;
  4. import android.database.Cursor;
  5. import android.os.Bundle;
  6. import android.provider.ContactsContract;
  7. import android.widget.SimpleAdapter;
  8. import android.widget.Toast;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11.  
  12.  
  13. public class TestContacts extends ListActivity {
  14.  
  15.  
  16.         private ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
  17.  
  18.         private SimpleAdapter numbers;
  19.  
  20.         /** Called when the activity is first created. */
  21.         @Override
  22.         public void onCreate(Bundle savedInstanceState) {
  23.                 super.onCreate(savedInstanceState);
  24.                 setContentView(R.layout.contacts);
  25.                 numbers = new SimpleAdapter(
  26.                                 this,
  27.                                 list,
  28.                                 R.layout.main_item_two_line_row,
  29.                                 new String[] { "line1","line2" },
  30.                                 new int[] { R.id.text1, R.id.text2 }  );
  31.                 setListAdapter( numbers );
  32.  
  33.  
  34.  
  35.                 Cursor cursor = getContentResolver().query(ContactsContract.Contacts.CONTENT_URI,
  36.                                 null, null, null, null);
  37.                 while (cursor.moveToNext()) {
  38.                         String contactId = cursor.getString(cursor.getColumnIndex(
  39.                                         ContactsContract.Contacts._ID));
  40.                         String hasPhone = cursor.getString(cursor.getColumnIndex(
  41.                                         ContactsContract.Contacts.HAS_PHONE_NUMBER));
  42.  
  43.                         //check if the contact has a phone number
  44.                         if (Boolean.parseBoolean(hasPhone)) {
  45.                                
  46.                                 Cursor phones = getContentResolver().query(
  47.                                                 ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
  48.                                                 null,
  49.                                                 ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,
  50.                                                 null, null);
  51.                                 while (phones.moveToNext()) {
  52.                                         // Get the phone number!?
  53.  
  54.                                         String contactName = phones.getString(
  55.                                                         phones.getColumnIndex(
  56.                                                                         ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
  57.  
  58.                                         String phoneNumber = phones.getString(
  59.                                                         phones.getColumnIndex(
  60.                                                                         ContactsContract.CommonDataKinds.Phone.NUMBER));
  61.                                         Toast.makeText(this, phoneNumber, Toast.LENGTH_LONG).show();                                   
  62.  
  63.                                         drawContact(contactName, phoneNumber);
  64.  
  65.                                 }
  66.                                 phones.close();
  67.                         }
  68.                 }cursor.close();
  69.         }
  70.  
  71.         private void drawContact(String name, String number){
  72.  
  73.                 HashMap<String,String> item = new HashMap<String,String>();
  74.                 item.put( "line1",name);
  75.                 item.put( "line2",number);
  76.                 list.add( item );
  77.                 numbers.notifyDataSetChanged();
  78.  
  79.         }
  80.  
  81. }
  82.  
  83.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4


It'seems that no contact have a phone number (i've added 2 contacts on the emulator and i've tried also on my HTC Desire). The problem is that
if (Boolean.parseBoolean(hasPhone))
returns always false..
How can i get correctly phone numbers?

I've tried to call drawContact(String name, String number) before the if statement without querying for the phone number, and it worked (it draws two times the name). but on the LinearLayout they are not ordered alphabetically... how can i order alphabetically (similar to the original contacts app)?

thank you in advice,
Luca
PolarUrso
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 10, 2010 5:47 pm

Top

Re: get Phone numbers

Postby Chitra » Fri Jun 11, 2010 2:20 pm

Hi polar,
Just try it...

private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}

private String getMy10DigitPhoneNumber(){
String s = getMyPhoneNumber();
return s.substring(2);
}
Thanks,
Chitra
User avatar
Chitra
Master Developer
Master Developer
 
Posts: 213
Joined: Mon Mar 01, 2010 7:59 am
Location: Bangalore

Re: get Phone numbers

Postby PolarUrso » Fri Jun 11, 2010 2:49 pm

i found the solution
I've changed line 44 into if (hasPhone.equals("1")) {
...

now it works.
The only problem now is the alphabetic order.. but i'm wondering that i've used the wrong view. But i don't find the view that default contacts app uses.. any idea?
PolarUrso
Freshman
Freshman
 
Posts: 2
Joined: Thu Jun 10, 2010 5:47 pm

Re: get Phone numbers

Postby fbarnard » Tue Jun 15, 2010 2:43 pm

HI

The getline1number is not supported anymore android 2.1.
fbarnard
Developer
Developer
 
Posts: 44
Joined: Tue Jun 15, 2010 12:31 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 17 guests