How to fetch recent and favourite contacts in android?

为君一笑 提交于 2019-12-25 09:19:59

问题


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

回答1:


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


来源:https://stackoverflow.com/questions/43715236/how-to-fetch-recent-and-favourite-contacts-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!