0

I m making one app in which I want to have all records from my android mobile 4.0 to my android application. I have done this also. but problem is I have almost 200 contacts in my phonebook but I m getting only 90 records randomly in my application. I have tried a lot. but nothing solution I found out. can any one has solution? below is my code :

ContentResolver cr=getContentResolver();
Cursor cur=cr.query(ContactsContract.Contacts.CONTENT_URI,null,null,null,null);
if (cur.getCount() > 0) 
{
    while (cur.moveToNext()) 
    {
     String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
     String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
     if(Integer.parseInt(cur.getString 
                  (cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER )))  > 0) 
     {
       //Query phone here.  Covered next
    Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                      null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", new 
                      String[]{id}, null);
        while (pCur.moveToNext()) 
       {
                // Do something with phones
    String pnumber 
        =pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
    contactids.add(id);
    names.add(name.trim().toLowerCase());
    PhoneNumbers.add(pnumber);
    } 
       pCur.close();
}
else
{
    String pnumber="";
    contactids.add(id);
    names.add(name);
    PhoneNumbers.add(pnumber);
}
}

finally I have done with following code. But the problem with this is it can't fetch records if contacts are more then 1500. and till 1500 records process is very slow.

public class MyActivity extends Activity {
public Cursor cur;
public int j=0;
@Override
public void onCreate(Bundle savedInstanceState) 
{
     cur=getContacts();
    startManagingCursor(cur);
    cur.moveToFirst(); 
    Button btn=(Button)findViewById(R.id.button1);
    btn1.setOnClickListener(new OnClickListener() 
    {   
        public void onClick(View v) 
        {               
            contactids=new ArrayList<String>();
            names=new ArrayList<String>();
            PhoneNumbers=new ArrayList<String>();
            try
            {
                new showdialog1(MyActivity.this).execute();
            }
            catch (Exception e) 
        {
            // TODO: handle exception
            e.printStackTrace();
        }
        }
    });
}
class showdialog1 extends AsyncTask<Void, Void, Void>
{
    public showdialog1(Activity act) {
         super.onPreExecute();
    }   
    @Override
    protected void onPreExecute() {

        dialog = ProgressDialog.show(MyActivity.this,"title", "message");
    }

    @Override
    protected Void doInBackground(Void... params) {
        if (cur.getCount() > 0) 
        {
            do
            {
                String id = cur.getString(
                cur.getColumnIndex(ContactsContract.Contacts._ID));
                String name = cur.getString(
                                cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                Cursor pCur = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,ContactsContract.CommonDataKinds.Phone.CONTACT_ID +"=" +id, null, null);
                if(pCur.getCount()>0)
                {
                    while (pCur.moveToNext()) 
                        {
                        // Do something with phones
                            String pnumber=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                            contactids.add(id);
                            names.add(name.trim().toLowerCase());
                            PhoneNumbers.add(pnumber);
                        } 
                        pCur.close();
            }
            else
            {
                String pnumber="";
                    contactids.add(id);
                    names.add(name.trim().toLowerCase());
                    PhoneNumbers.add(pnumber);
            }

           }while (cur.moveToNext()); 
            cur.close();
        }               
        return null;        
    }
    @Override
    protected void onPostExecute(Void result) {
        dialog.dismiss();
        int contactidsize=contactids.size();
        int namesize=contactids.size();
        int numbersize=contactids.size();
        saverecords();
        return;
    }       

}
}
Lain
  • 2,166
  • 4
  • 23
  • 47
lakhani
  • 128
  • 2
  • 13

3 Answers3

2

you get your contact list of your phone.. try this code and after that if you want to use contact from list you select that contace and use details of that contact (ex: number, name ,email.id etc..) plz goto below link

/***  USE this CODE   ******/
int PICK_CONTACT=1;

Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, PICK_CONTACT);`
Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
Sagar Patel
  • 163
  • 1
  • 6
1

You are using the condition:

Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER ))) > 0)

which does not consider the contacts that doesnot have phone numbers .

Hope this works

Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
arul
  • 190
  • 2
  • 15
-1

See ContactManager http://developer.android.com/resources/samples/ContactManager/index.html

Create and initialize boolean variable mShowInvisible - if set to true, it will list all contacts regardless of user preference

/**
* Obtains the contact list for the currently selected account.
*
* @return A cursor for for accessing the contact list.
*/
private Cursor getContacts(){
// Run query
Uri uri = ContactsContract.Contacts.CONTENT_URI;
String[] projection = new String[] {
    ContactsContract.Contacts._ID, ContactsContract.Contacts.DISPLAY_NAME
};
String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"(mShowInvisible ? "0" : "1") + "'";
String[] selectionArgs = null;
String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

return managedQuery(uri, projection, selection, selectionArgs, sortOrder);
}
Marcel Bro
  • 4,907
  • 4
  • 43
  • 70
Shekhar Chikara
  • 3,786
  • 2
  • 29
  • 52