title: How to fetch recent and favourite contacts in android?
body: <p>I have been trying to fetch recent and favorites contacts but every time i get error .
  i am storing contacts in database after fetching .</p>
  
  <blockquote>
    <p>cannot read column -1
    and  sometimes it says cursor not initialized properly.</p>
  </blockquote>
  
  <p>please help me .</p>
  
  <p><strong>Here is my code.</strong></p>
  
  <pre><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() &gt; 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))) &gt; 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();
      }
  </code></pre>