CursorLoader 结果选择

CursorLoader result selection

我构建了一个可以显示我所有联系人的 Android 应用程序。 这是我的光标的样子:

public Cursor getAllContacts()
{
    CursorLoader cl = new CursorLoader(context, ContactsContract.Contacts.CONTENT_URI, null, null, null, ContactsContract.Contacts.DISPLAY_NAME);
    return cl.loadInBackground();
}

它工作正常,但我想构建第二个 Cursor,它只能显示我在 SharedPreferences 中设置的收藏夹。

这是我停留的地方:

    public Cursor getFavContacts(){



    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    Map<String, ?> allEntries = sharedPrefs.getAll();

    for (Map.Entry<String, ?> entry : allEntries.entrySet()) {

        if(entry.getValue() instanceof Boolean) {
            Boolean isFav = (Boolean)entry.getValue();
            if(isFav) {
                Log.d("map values", entry.getKey() + ": " + entry.getValue().toString());

                String[] split = entry.getKey().split("_");
                Log.d("splittedvalue", split[1]);
            }
        }
    }


    CursorLoader cl = new CursorLoader(context, ContactsContract.Contacts.CONTENT_URI,null,null,null,ContactsContract.Contacts.DISPLAY_NAME);


    Cursor cursor = cl.loadInBackground();
    if(cursor != null) {
        int count = cursor.getCount();
        Log.d("ContactsAdapter", "count: "  + count);



    }


    return cl.loadInBackground();
}

我的日志只正确显示了我想要得到的结果。 现在我想告诉 CursorLoader 只获取我的收藏夹。 我该怎么做?

您应该在 CursorLoader 中添加一些 selection

如果你想获得phone设置的收藏夹(a.k.a已加星标的联系人在Android的联系人列表中)标准是 ContactsContract.Contacts.STARRED

如果您只想显示您在应用中挑选出的特定联系人(或在您的应用中收藏的联系人),那么我猜您已经将这些联系人的 _IDDISPLAY_NAME 保存在您的SharedPreferences,因此您将在选择标准中使用它。

例如,要显示 已加星标 联系人,您的 CursorLoader 可以如下所示:

CursorLoader cl = 
     new CursorLoader(context,
          ContactsContract.Contacts.CONTENT_URI,
          null,
          ContactsContract.Contacts.STARRED = "1"
          null,
          ContactsContract.Contacts.DISPLAY_NAME);

P.S。我没有运行上面的代码,不过你应该可以从中把握大概的思路。

找到解决方案了!

    public Cursor getFavContacts(){

    String selection = null ;
    String[] split = null;
    SharedPreferences sharedPrefs = PreferenceManager.getDefaultSharedPreferences(context);

    Map<String, ?> allEntries = sharedPrefs.getAll();
    StringBuffer sb = new StringBuffer();
    final String COMMATA = ",";

    sb.append( ContactsContract.Contacts._ID+" IN (");


    for (Map.Entry<String, ?> entry : allEntries.entrySet()) {

        if(entry.getValue() instanceof Boolean) {
            Boolean isFav = (Boolean)entry.getValue();
            if(isFav) {

                split = entry.getKey().split("_");
                sb.append(split[1]);
                sb.append(COMMATA);

            }
        }
    }
    int sblaenge = sb.length();
    sb.delete(sblaenge-1,sblaenge);
    sb.append( ")" );
    selection = sb.toString();
    Log.d(selection, selection);

    CursorLoader cl = new CursorLoader(context, ContactsContract.Contacts.CONTENT_URI,null,selection,null,ContactsContract.Contacts.DISPLAY_NAME);


    Cursor cursor = cl.loadInBackground();
    if(cursor != null) {
        int count = cursor.getCount();
        Log.d("ContactsAdapter", "count: "  + count);

    }
    return cl.loadInBackground();
}

我创建了一个选择字符串,其中包含一个获取相关联系人 ID 的 sqlite 查询。