0

I am creating an app which gets ALL Names, Phone-Numbers and Email of every specific Name and i'm kinda stuck.. I can get all the names and emails so far but cannot get all three.. I have seen some questions related to the topic but couldn't get help in getting all three at the same time. I'm saving names,phone and emails in a binary tree.. I'm a beginner.Need help!

here's my code:

    package com.example.contactify;

import android.content.ContentProviderOperation;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.database.Cursor;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import com.example.Tree.ContactNode;

public class MainPage extends Activity implements OnClickListener {

    ProgressBar progress;
    TextView tv;
    ContactNode t;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.mainmenu);
        Button createmagic = (Button) findViewById(com.example.contactify.R.id.cButton);
        tv = (TextView) findViewById(R.id.textView3);
        createmagic.setOnClickListener(this);
        t = new ContactNode();

    }


    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        switch (arg0.getId()) {
            case R.id.cButton: {
                try {

                    ContactNode contact = getContacts();
                    Intent i = new Intent("com.example.contactify.CreateNames");
                    i.putExtra("Contact", Names(t.root));
                    startActivity(i);


                } catch (Exception e) {
                    // TODO Auto-generated catch block
                    Toast.makeText(this, e.getLocalizedMessage().toString(), Toast.LENGTH_LONG).show();
                }
                break;
            }
            case R.id.uButton: {

                break;
            }

        }
    }


    String Names(ContactNode tree) {
        return tree.getNames(tree);
    }


    ContactNode getContacts() {

        final String[] PROJECTION = new String[]{
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Email.DATA,
                ContactsContract.CommonDataKinds.Phone.NUMBER
        };

        ContentResolver cr = getContentResolver();
        Cursor cursor = cr.query(ContactsContract.Contacts.CONTENT_URI, PROJECTION, null, null, null);
        if (cursor != null) {
            try {
                final int contactIdIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.CONTACT_ID);
                final int displayNameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);
                final int emailIndex = cursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA);
                long contactId;
                String displayName, address, phoneNo;


                while (cursor.moveToNext()) {

                  //  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI,null, null, null, null);
                    if (Integer.parseInt(cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) {

                        contactId = cursor.getLong(contactIdIndex);
                        displayName = cursor.getString(displayNameIndex);
                        address = cursor.getString(emailIndex);

                        Cursor phones = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactIdIndex,null, null);
                        while (phones.moveToNext())
                        {
                            phoneNo = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            t.insertContact(displayName, phoneNo, address);
                        }
                        phones.close();

                    }
                }
            } finally {
                cursor.close();
            }

        }
        return t;
    }

}

U. Ahmad
  • 76
  • 1
  • 11

1 Answers1

0

Phone number has varies types in android. Like home, mobile, office etc., So you are adding same persons phone number multiple time with null values.

ref: http://developer.android.com/reference/android/provider/Contacts.PhonesColumns.html

ref:http://developer.android.com/reference/android/provider/ContactsContract.CommonDataKinds.Phone.html

Thats why you also using phone cursor to retrieve phone numbers. A contact may have 0 or more phone numbers. you need to decide which one is you want. If you want any one than use this

 Cursor phones =     getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ contactIdIndex,null, null);
 phoneNo = null;
 while (phones.moveToNext())
 {
    phoneNo = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));                           
 }
 phones.close();

 //this is if the phone exist then only it added
 if(phoneNo != null)
     t.insertContact(displayName, phoneNo, address);
arun
  • 1,728
  • 15
  • 15