0

I have been trying to fetch recent and favorites contacts but every time i get error . i am storing contacts in database after fetching .

cannot read column -1 and sometimes it says cursor not initialized properly.

please help me .

Here is my code.

 ContentResolver cr = getActivity().getContentResolver();
  /*  Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, null,
            null, null, null );*/


  Cursor cur=cr.query(CallLog.Calls.CONTENT_URI,null,CallLog.Calls.DATE, null,null);
    String phone = null;
    String emailContact = null;
    String image_uri;
    Bitmap bitmap;

    final SQLiteDatabase mDb = db.getWritableDatabase();
    mDb.beginTransaction();

    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));

            image_uri = cur
                    .getString(cur
                            .getColumnIndex(ContactsContract.CommonDataKinds.Phone.PHOTO_URI));
            if (Integer
                    .parseInt(cur.getString(cur
                            .getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0)
            {
                System.out.println("name : " + name + ", ID : " + id);

                Cursor pCur = cr.query(
                        ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
                        null,
                        ContactsContract.CommonDataKinds.Phone.CONTACT_ID
                                + " = ?", new String[]{id}, null);
                Log.e("pCur","dfgfdg  "+pCur.getCount());
                while (pCur.moveToNext())
                {
                    phone = pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
                    // contactid=pCur.getString(pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));

                   /* phonenumber.add(pCur
                            .getString(pCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)));`*/

                }
                pCur.close();


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

                while (emailCur.moveToNext())
                {
                    emailContact = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));

                    if(TextUtils.isEmpty(emailContact)||emailContact.equalsIgnoreCase(null)||emailContact.equalsIgnoreCase(""))
                    {
                        emailContact="";

                        Log.e("isEmpty","isEmpty " + emailContact);
                    }

                    else
                    {
                        Log.e("gfdszfg","Email " + emailContact);
                    }
                  /*  emailType = emailCur
                            .getString(emailCur
                                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));*/

                    Log.e("gfdszfg","Email " + emailContact);
                }
                emailCur.close();
            }

            if (image_uri != null)
            {
                System.out.println(Uri.parse(image_uri));
                try
                {
                    bitmap = MediaStore.Images.Media
                            .getBitmap(getActivity().getContentResolver(),
                                    Uri.parse(image_uri));
                    System.out.println(bitmap);

                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

            mList.add(new Contacts(name, phone, image_uri,emailContact));

            ContentValues contentValues = new ContentValues();
            contentValues.put("contact_name", name);
            contentValues.put("contact_number",phone);
            contentValues.put("contact_email",emailContact);
            contentValues.put("contact_image",image_uri);
            mDb.insert(TABLE_CONTACT, null, contentValues);

            emailContact="";
            phone="";
        }
        mDb.setTransactionSuccessful();

        mDb.endTransaction();
        cur.close();
    }

2 Answers2

0

You're querying the CallLog table:

Cursor cur=cr.query(CallLog.Calls.CONTENT_URI,null,CallLog.Calls.DATE, null,null)

and then trying to get info from that cursor with fields from the Contacts table:

cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID));

Obviously, that's not how it should work.

Also, your selection:

CallLog.Calls.DATE

is not a legal selection string.

How you should do it:

To get a list of starred (favourites) contacts:

String[] projection = new String[] { Contacts._ID }; // you can add more fields you need here
Cursor cursor = cr.query(Contacts.CONTENT_URI, projection, Contacts.STARRED + "=1", null, null);

To get a list of contacts with in the last 24 hours:

String[] projection = new String[] { Contacts._ID }; // you can add more fields you need here
int oneDay = (1000 * 60 * 60 * 24);
long last24h = (System.currentTimeMillis() - oneDay);
Cursor cursor = cr.query(Contacts.CONTENT_URI, projection, Contacts.    LAST_TIME_CONTACTED + ">" + last24h, null, null);
marmor
  • 27,641
  • 11
  • 107
  • 150
  • code for fetching last 24 hours contact works but it only shows my saved contacts it does not show newly dialed numbers. – Prashant Jaiswal May 01 '17 at 12:50
  • when i try to run code for favorite contacts it gives me error `Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it.` – Prashant Jaiswal May 01 '17 at 12:55
  • `col -1` means you're trying to access a field that is not in the projection, you need to add fields you want to access to the projection array – marmor May 01 '17 at 13:24
  • the code you posted also assumed all the numbers in the call log are contacts, as it tried to fetch contacts for each phone. if you just want last called numbers, then it's a simple query to the call-log table – marmor May 01 '17 at 13:26
  • so what should i do to fetch favourite contacts – Prashant Jaiswal May 02 '17 at 07:52
  • the code in the answer should fetch all starred (favorite) contacts, if you're getting `col -1` errors, just add fields to the projection – marmor May 03 '17 at 07:23
0

I know till now you may have solved that error but still i want to answer for further help for seekers. The code for favorite contact fetching is working you just need to add following in the projection:

String[] projection = new String[] { ContactsContract.Contacts._ID,
                    ContactsContract.Contacts.LOOKUP_KEY,
                    ContactsContract.Contacts.HAS_PHONE_NUMBER,
                    ContactsContract.Contacts.DISPLAY_NAME_PRIMARY};