简体   繁体   中英

Getting details of only those contacts whose birthday exists in the contact database (Android)

I am trying to query the contacts provider and fetch information of contacts, so far I am successful in listing this information, however I am more interested in retrieving the information of those contacts who have birthdates specified. So far my code looks as follows:

    public class LoaderClass extends Activity implements LoaderCallbacks<Cursor>{

    // private ProgressDialog pDialog;
    // Async as;
    TextView t1, t2;
    Locale current; 
    Uri uri;
    String[] projection;
    String where;

    public static final int progress_bar_type = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.loader_class);

        t1 = (TextView) findViewById(R.id.textView1);
        t2 = (TextView) findViewById(R.id.textView2);
        t1.setText("");
        current = getResources().getConfiguration().locale;
        // new Async().execute();

        uri = ContactsContract.Data.CONTENT_URI;

        projection = new String[] {
                // take from the contacts
                ContactsContract.Contacts.DISPLAY_NAME,
                ContactsContract.CommonDataKinds.Event.CONTACT_ID,
                ContactsContract.CommonDataKinds.Event.START_DATE,
                ContactsContract.CommonDataKinds.Phone.NUMBER,
                ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS,
                ContactsContract.CommonDataKinds.Photo.PHOTO


        };

         where = ContactsContract.Data.MIMETYPE + "= ? AND "
                + ContactsContract.CommonDataKinds.Event.TYPE + "="
                + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;


        String[] selectionArgs = new String[] { ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE };

        getLoaderManager().initLoader(0, null, this);


    }

    @Override
    public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
        CursorLoader loader = new CursorLoader(
                this,
                uri, 
                projection, 
                null, 
                null, 
                null);
        return loader;
    }

    @Override
    public void onLoadFinished(
            Loader<Cursor> loader, 
            Cursor cursor) {


        while (cursor.moveToNext()) {

            String displayBirthday = cursor
                    .getString(cursor
                            .getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));

            String name = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));


            String ename = cursor.getString(cursor
                    .getColumnIndex(ContactsContract.CommonDataKinds.Email.ADDRESS));


            t1.append("\n"+displayBirthday+"\n"+name+"\n"+ename);
        }


    }

    @Override
    public void onLoaderReset(Loader<Cursor> arg0) {
        // TODO Auto-generated method stub

    }

}

I want to design a where clause, but I am not able to comprehend how.

Please use below query for these:

String[] projectionFields = new String[]{
        ContactsContract.Contacts._ID,
        ContactsContract.Contacts.DISPLAY_NAME,
};
ArrayList<Contact> listContacts = new ArrayList<>();
CursorLoader cursorLoader = new CursorLoader(context,
        ContactsContract.Contacts.CONTENT_URI,
        projectionFields, // the columns to retrieve
        null, // the selection criteria (none)
        null, // the selection args (none)
        null // the sort order (default)
);

Cursor c = cursorLoader.loadInBackground();

final Map<String, SynchData.clientsData> contactsMap = new HashMap<>(c.getCount());

if(c.moveToFirst()){

    int idIndex = c.getColumnIndex(ContactsContract.Contacts._ID);
    int nameIndex = c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME);

    do {
        String contactId = c.getString(idIndex);

        String where = ContactsContract.Data.CONTACT_ID + " = ? AND " + ContactsContract.Data.MIMETYPE + " = ? AND " +
                ContactsContract.CommonDataKinds.Event.TYPE + " = " + ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
        String[] selectionArgs = new String[]{id, ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE};

        Cursor birthDateCur = cr.query(ContactsContract.Data.CONTENT_URI, null, where, selectionArgs, null);

        while (birthDateCur.moveToNext()) {
            birthDate = birthDateCur.getString(birthDateCur.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE));
            if (birthDate != null && !birthDate.equalsIgnoreCase("")) {
                if (!birthDate.equalsIgnoreCase("")) {
                    print(" birthDate " + birthDate);
                    break;
                }
            }
        }
        birthDateCur.close();


    } while (c.moveToNext());
}
c.close();
// method to get name, contact id, and birthday
private Cursor getContactsBirthdays() {
Uri uri = ContactsContract.Data.CONTENT_URI;

String[] projection = new String[] {
        ContactsContract.Contacts.DISPLAY_NAME,
        ContactsContract.CommonDataKinds.Event.CONTACT_ID,
        ContactsContract.CommonDataKinds.Event.START_DATE
};

String where =
        ContactsContract.Data.MIMETYPE + "= ? AND " +
        ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
        ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;
String[] selectionArgs = new String[] { 
    ContactsContract.CommonDataKinds.Event.CONTENT_ITEM_TYPE
};
String sortOrder = null;
return managedQuery(uri, projection, where, selectionArgs, sortOrder);
}

// iterate through all Contact's Birthdays and print in log
Cursor cursor = getContactsBirthdays();
   int bDayColumn =      cursor.getColumnIndex(ContactsContract.CommonDataKinds.Event.START_DATE);
  while (cursor.moveToNext()) {
String bDay = cursor.getString(bDayColumn);
Log.d(TAG, "Birthday: " + bDay);
}

Make where clause:

    String where =
    ContactsContract.Data.MIMETYPE + "= ? AND " +
    ContactsContract.CommonDataKinds.Event.TYPE + "=" + 
    ContactsContract.CommonDataKinds.Event.TYPE_BIRTHDAY;

And replace your below line:

@SuppressWarnings("deprecation")
    Cursor cursor = managedQuery(uri, projection, null, null,
            sortOrder);

with this

Cursor cursor = managedQuery(uri, projection, where, null,
            sortOrder);

Don't panic, just make one list from all contacts. Step 2 :Now I think you take all contact info, Step 3: save it in one wrapper class, then:

ArrayList<String> matchname = new ArrayList<String>();
                for(int k =0;k<yourcontactwrapperlist.size();k++)
                {

                        String keyname = yourcontactwrapperlist.get(i).yourgettermethodnameforbirthday();

                        if((keyname.trim().size()<0))
                        {
                            matchname.add(keyname);

                        }
                    }
}

And then in temp list you get all data which you want to display.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM