Tony
Tony

Reputation: 1185

android, I try to find what type of number(from contact) is (mobile, home,work)

String tmp = txtPhoneName.getText().toString();
ContentResolver cr = getContentResolver(); 
Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, null,  "DISPLAY_NAME = '" + tmp + "'", null, null);

if (cursor.moveToFirst()) {     
    String contactId = Cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
    Cursor phones = cr.query(Phone.CONTENT_URI, null, Phone.CONTACT_ID + " = " + contactId, null, null); 

    while (phones.moveToNext()) {   
        String number = phones.getString(phones.getColumnIndex(Phone.NUMBER));
        number1 = number;
        int type = phones.getInt(phones.getColumnIndex(Phone.TYPE));  

        switch (type) {    
            case Phone.TYPE_HOME: 
            // do something with the Home number here...  
                break;          
            case Phone.TYPE_MOBILE:    
                // do something with the Mobile number here...   
                break;            
            case Phone.TYPE_WORK:   
                // do something with the Work number here... 
                break;           
        }   
    }    
    phones.close();
}

My issue is that when it is at the switch statment it doesn't check anything. It goes to the while(phones.moveToNext()) where is my error does Phone.TYPE_HOME is not int and my type can't compare to it... And one last question, why for some contacts it gets the mobile number and for others the home (and in both cases the contacts have a home and a mobile number)? I want to get a number that starts with 07 and if there is no that kind of number to get whatever number there is for the contact, and then how to call that number... THX

Upvotes: 5

Views: 3142

Answers (3)

Sai Ganesh Pittala
Sai Ganesh Pittala

Reputation: 199

If you have got Phone.TYPE_CUSTOM

you can try the below statement for fetching the custom label.

phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.LABEL)))           

Upvotes: 0

Pir Fahim Shah
Pir Fahim Shah

Reputation: 10633

Here i am going to show you that how to get home,mobile and work phone no of any contact, First of all u get an uri of any contact_id and then use below method to get all phone type nos.

while (phone_crsr.moveToNext()) 
{ 
 int phone_type = phone_crsr.getInt(phone_crsr.getColumnIndex(Phone.TYPE));  
  switch (phone_type) 
     {    
      case Phone.TYPE_HOME: 
       phone_home =phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
       Toast.makeText(this, "home"+phone_home, Toast.LENGTH_LONG).show();
       break;          
      case Phone.TYPE_MOBILE:     
       phone_mob=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
       Toast.makeText(this, "mob"+phone_mob, Toast.LENGTH_LONG).show();  
       break;            
      case Phone.TYPE_WORK:                                  
       phone_work=phone_crsr.getString(phone_crsr.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
       Toast.makeText(this, "work"+phone_work, Toast.LENGTH_LONG).show();
       break;           
     }
 }

Upvotes: 11

denza
denza

Reputation: 1298

It is probably because u haven put anything yet in the cases, try with int i=0; and see in the debugger

Upvotes: 0

Related Questions