仅获取联系人数据库(Android)中存在生日的联系人的详细信息

时间:2013-11-27 09:06:53

标签: android android-contacts

我正在尝试查询联系人提供商并获取联系人信息,到目前为止我已成功列出此信息,但我更感兴趣的是检索那些已指定生日的联系人的信息。到目前为止,我的代码如下:

    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

    }

}

我想设计一个where子句,但我无法理解如何,任何帮助都会受到高度赞赏。

4 个答案:

答案 0 :(得分:1)

请使用以下查询:

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

答案 1 :(得分:0)

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

答案 2 :(得分:0)

制作where子句:

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

并替换以下行:

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

用这个

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

答案 3 :(得分:-1)

别恐慌花花公子只需从所有联系方式列出一个列表哦,我想你把所有的联系信息保存在一个包装类中然后

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

                        }
                    }
}

然后在临时列表中你得到你想要显示的所有数据我认为它会帮助你