2

I am trying to get the contacts name and their types but getting this exception at line marked in the code.I was getting the names prior to adding the types,but now getting this exception.please help.thanks in advance.

 package application.test;
import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;

import android.provider.ContactsContract;
import android.provider.ContactsContract.CommonDataKinds.Phone;
import android.provider.ContactsContract.Contacts;
import android.util.Log;

public class TestActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        String[] projection = new String[] { ContactsContract.Contacts._ID,
                ContactsContract.Contacts.DISPLAY_NAME,Phone.TYPE};     

        ContentResolver cr = getContentResolver();
        ContentResolver ncr=getContentResolver();

        Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");
        Cursor ncur=ncr.query(ContactsContract.Data.CONTENT_URI, projection, null, null,Contacts.DISPLAY_NAME + " ASC");
        Cursor icur = cr.query(ContactsContract.RawContacts.CONTENT_URI, projection,null, null, Contacts.DISPLAY_NAME + " ASC");

        if (cur.getCount() >0 && ncur.getCount()>0) 
        {
        while (cur.moveToNext()&& ncur.moveToNext()) 
        {

    String id = icur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));
    String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
    String type=ncur.getString(cur.getColumnIndex(Phone.TYPE))  ;

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


Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null, 
              ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?",new String[]{id}, null);

Cursor typecur = ncr.query( ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null,
                ContactsContract.CommonDataKinds.Phone.CONTACT_ID + " = ?", new String[]{id}, null);             


while (pCur.moveToNext()&& typecur.moveToNext())
{

                Log.d("names",name);
                Log.d("types",type);
              pCur.close();
} 
}
}
}
}
}
picaso
  • 713
  • 2
  • 14
  • 26

1 Answers1

0

In projection, Phone.Type is not a part of Contacts Table which you are querying. Explain in detail, what you are trying.

If you are looking at Type of Phone, then you should be querying Data table

NOTE: I presume that you are using Android version 2.0 + and not 1.5 or 1.6

Vinay
  • 2,395
  • 3
  • 30
  • 35
  • 2
    I am trying to retrieve names stored in contacts and their respective types i.e(home,work,mobile).. – picaso Nov 09 '11 at 08:54
  • Names are available in Contacts table, Type info is available only in Data table. You need to query 3 different table to get the info. 1. Contacts table, Raw Contacts Table and Data table – Vinay Nov 09 '11 at 08:56
  • why raw contacts table..could you please explain this thing a little more. – picaso Nov 09 '11 at 09:05
  • Data table stores all kinds of info with Raw Contacts id. Raw contacts table stores, contact id – Vinay Nov 09 '11 at 09:08
  • I tried querying different tables but same error at same line.... see the edited code....... – picaso Nov 09 '11 at 09:36
  • change projection for each table. – Vinay Nov 09 '11 at 10:09
  • now getting cursor index out of bound exception index-1 requested with size of 10 – picaso Nov 09 '11 at 10:22
  • I saw your other question you removed. 1st query for Raw contacts, then get the Phone type if available from Data table. Once you have this info, query for contacts table for display_name. – Vinay Nov 09 '11 at 11:25