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

仅获取联系人数据库中生日存在的联系人的详细信息(Android),android,android-contacts,Android,Android Contacts,我正在尝试查询联系人提供商并获取联系人信息,到目前为止,我成功地列出了这些信息,但是我更感兴趣的是检索那些指定了生日的联系人的信息。到目前为止,我的代码如下所示: public class LoaderClass extends Activity implements LoaderCallbacks<Cursor>{ // private ProgressDialog pDialog; // Async as; TextView t1, t2;

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

    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

    }

}
公共类LoaderClass extends活动实现LoaderCallbacks{
//私人对话;
//异步as;
文本视图t1、t2;
现场电流;
Uri;
字符串[]投影;
字符串在哪里;
公共静态最终整数进度条类型=0;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.loader_类);
t1=(TextView)findViewById(R.id.textView1);
t2=(TextView)findViewById(R.id.textView2);
t1.setText(“”);
当前=getResources().getConfiguration().locale;
//新建异步().execute();
uri=contacts contract.Data.CONTENT\u uri;
投影=新字符串[]{
//从联系人处获取
Contacts contract.Contacts.DISPLAY\u NAME,
ContactsContract.CommonDataTypes.Event.CONTACT\u ID,
ContactsContract.CommonDataTypes.Event.START\u日期,
Contacts contract.CommonDataTypes.Phone.NUMBER,
Contacts contract.CommonDataTypes.StructuredPostal.FORMATTED\u地址,
Contacts contract.CommonDataTypes.Photo.Photo
};
其中=contacts contract.Data.MIMETYPE+“=”和“
+ContactsContract.CommonDataTypes.Event.TYPE+“=”
+Contacts contract.CommonDataTypes.Event.TYPE\u生日;
String[]SelectionAgs=新字符串[]{ContactsContract.CommonDataTypes.Event.CONTENT\u ITEM\u TYPE};
getLoaderManager().initLoader(0,null,this);
}
@凌驾
公共加载器onCreateLoader(int arg0,Bundle arg1){
游标装入器装入器=新游标装入器(
这
乌里,
投影,
无效的
无效的
无效);
返回装载机;
}
@凌驾
已完成公共无效加载(
装载机,
光标(光标){
while(cursor.moveToNext()){
字符串显示生日=光标
.getString(游标
.getColumnIndex(ContactsContract.CommonDataTypes.Event.START_DATE));
字符串名称=cursor.getString(cursor
.getColumnIndex(Contacts contract.Contacts.DISPLAY_NAME));
字符串ename=cursor.getString(cursor
.getColumnIndex(ContactsContract.CommonDataTypes.Email.ADDRESS));
t1.追加(“\n”+显示生日+”\n”+名称+“\n”+名称);
}
}
@凌驾
公共void onLoaderReset(加载程序arg0){
//TODO自动生成的方法存根
}
}

我想设计一个where子句,但我无法理解如何设计。

不要惊慌,只需从所有联系人中列出一个列表即可。第2步:现在我认为您获取了所有联系人信息,第3步:将其保存在一个包装类中,然后:

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

                        }
                    }
}
ArrayList matchname=new ArrayList();
对于(int k=0;kMake 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);

请使用以下查询查询这些:

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();
String[]projectionFields=新字符串[]{
联系人合同联系人。\u ID,
Contacts contract.Contacts.DISPLAY\u NAME,
};
ArrayList listContacts=新的ArrayList();
游标装入器游标装入器=新游标装入器(上下文,
Contacts contract.Contacts.CONTENT\u URI,
ProjectOnFields,//要检索的列
null,//选择条件(无)
null,//选择参数(无)
null//排序顺序(默认)
);
游标c=游标Loader.loadInBackground();
final-Map-contactsMap=新的HashMap(c.getCount());
if(c.moveToFirst()){
int idIndex=c.getColumnIndex(Contacts contract.Contacts.\u ID);
int nameIndex=c.getColumnIndex(ContactsContract.Contacts.DISPLAY\u NAME);
做{
字符串contactId=c.getString(idIndex);
字符串,其中=contacts contract.Data.CONTACT_ID+“=”和“+contacts contract.Data.MIMETYPE+”=”和“+
ContactsContract.CommonDataTypes.Event.TYPE+“=”+ContactsContract.CommonDataTypes.Event.TYPE\u;
String[]SelectionAgs=新字符串[]{id,ContactsContract.CommonDataTypes.Event.CONTENT\u ITEM\u TYPE};
游标birthDateCur=cr.query(contacts contract.Data.CONTENT\u URI,null,where,selectionArgs,null);
while(birthDateCur.moveToNext()){
birthDate=birthDateCur.getString(birthDateCur.getColumnIndex(ContactsContract.CommonDataTypes.Event.START_DATE));
if(birthDate!=null&!birthDate.equalsIgnoreCase(“”){
如果(!birthDate.equalsIgnoreCase(“”){
打印(“生日”+生日);
打破
}
}
}
birthDateCur.close();
}而(c.moveToNext());
}
c、 close();

不,我不想保存在列表中,然后再查找,这将是加倍的努力,这将使我的处理速度变慢,而带where子句的查询将对我起作用。“ohk”是什么意思mean?Mate我是在同一个教程中做的,但是如果我使用where子句,我会丢失其他字段的信息。我需要一个更健壮的where子句或一个指针来创建一个。你能解释一下where子句的作用吗?我使用了教程中指定的上述查询,但是它确实返回了生日联系人,但无法加载其他我感兴趣的信息。无论如何,我会再试一次。
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();