How to fetch recent and favourite contacts in android?


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 .

<strong>Here is my code.</strong>

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


You're querying the CallLog table:


Cursor cur=cr.query(<strong>CallLog.Calls.CONTENT_URI</strong>,null,CallLog.Calls.DATE, null,null)


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




Obviously, that's not how it should work.

Also, your selection:




is not a legal selection string.

<strong>How you should do it:</strong>

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


  • How do i upgrade my pre-populated sqlite database on my device without re-creating tables?
  • How to resolve OutOfMemoryError on Bitmap in Android?
  • Android SQLite column and index best practice
  • Android how to get attendance name and emails from events
  • How to close a cursor used in a for loop
  • Android Contacts on listview
  • Android: How to close a cursor that returns from Class to Activity
  • how to get the content fragment at onTabChanged(String tabTag)
  • Getting data from calendar on Android Wear
  • Progress Bar of DownloadManager with percentage
  • Implementing back stacks with fragments
  • Count how many strings in an array have duplicates in the same array [duplicate]
  • Select image from gridview and display it on other intent
  • Can not cast IMAPInputStream to Multipart
  • How to transform this native SQL query to an HQL
  • How to store items of multiple JSON Array and display it listview?
  • Is there a difference SMO ServerConnection transaction methods versus using the SqlConnectionObject
  • Hibernate session.beginTransaction() call and Mysql Start Transaction
  • TelephonyManager crashing on android studio
  • Get last call details from Call Log
  • GCM registration works for debug APK but not release APK
  • Android Login- checking if username and password match with MySQL database
  • How to extract text from a PDF and decode characters?
  • GridView breaks while scrolling
  • Why querying a date BC is changed to AD in Java?
  • Getting last autonumber in access
  • Counter field in MS Access, how to generate?
  • Incrementing object id automatically JS constructor (static method and variable)
  • How to check if every primary key value is being referenced as foreign key in another table
  • Can I have the cursor start on a particular column by default in jqgrid's edit mode?
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?