mr droid.
mr droid.

Reputation: 42

i am trying to display the contact name and number using listview however i can only display the name but number is not comming up

public class contacts extends ListActivity {    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try{
            setContentView(R.layout.con_main);
        }catch(Exception e){
            Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
        }

        try{
                Uri allContacts = Uri.parse("content://contacts/people");

                    Cursor c1 = managedQuery(allContacts,null,null,null,null);
            Cursor c = managedQuery(allContacts,null,null,null,null);
            String number = c.getString(c.getColumnIndex(
                    ContactsContract.CommonDataKinds.Phone.NUMBER));
            String[] columns = new String[] {
                ContactsContract.Contacts.DISPLAY_NAME,number

                 };
            int[] views = new int[] {R.id.contactName};
            SimpleCursorAdapter adapter =new SimpleCursorAdapter(this, R.layout.con_main, c1, columns, views);
            this.setListAdapter(adapter);



        }catch(Exception e){
            Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show();
        }

    }
}

Upvotes: 0

Views: 119

Answers (3)

Nikunj Patel
Nikunj Patel

Reputation: 22066

you can go trough this :: here you can get all contact information.

protected void getContactInfo(Intent intent)
        {
            try
            {
           Cursor cursor =  managedQuery(intent.getData(), null, null, null, null);      
           while (cursor.moveToNext()) 
           {           
               String contactId = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts._ID));
               String name = cursor.getString(cursor.getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME)); 
               Toast.makeText(getBaseContext(), "my name:: "+name, Toast.LENGTH_LONG).show();
               String hasPhone = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

               if ( hasPhone.equalsIgnoreCase("1"))
                   hasPhone = "true";
               else
                   hasPhone = "false" ;

               if (Boolean.parseBoolean(hasPhone)) 
               {
                Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactId,null, null);
                while (phones.moveToNext()) 
                {
                  String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                  Toast.makeText(getBaseContext(), "my msg:: "+phoneNumber, Toast.LENGTH_LONG).show();

                }
                phones.close();
               }

               // Find Email Addresses
               Cursor emails = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI,null,ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = " + contactId,null, null);
               while (emails.moveToNext()) 
               {
                String emailAddress = emails.getString(emails.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
                Toast.makeText(getBaseContext(), "my msg:: "+emailAddress, Toast.LENGTH_LONG).show();

               }
               emails.close();

            Cursor address = getContentResolver().query(
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.StructuredPostal.CONTACT_ID + " = " + contactId,
                        null, null);
            while (address.moveToNext()) 
            { 
              // These are all private class variables, don't forget to create them.
              String poBox = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POBOX));
              String street = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.STREET));
              String city = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.CITY));
              String state = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.REGION));
              String postalCode = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.POSTCODE));
              String country = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.COUNTRY));
              String type = address.getString(address.getColumnIndex(ContactsContract.CommonDataKinds.StructuredPostal.TYPE));
            }  //address.moveToNext()   
          }  //while (cursor.moveToNext())        
           cursor.close();
        //getContactInfo
        }catch (Exception e) {
            // TODO: handle exception
        }}

Upvotes: 0

Nixit Patel
Nixit Patel

Reputation: 4445

you need to define view like this

int[] views = new int[] { R.id.contactName, R.id.contactNumber };

that means you need to have 2 textview in your con_main layout file

Upvotes: 0

Mohamed_AbdAllah
Mohamed_AbdAllah

Reputation: 5322

This is because you are putting a single view for the adapter:

int[] views = new int[] {R.id.contactName}; 

You must add another TextView for the number

Upvotes: 1

Related Questions