CursorLoader结果选择

时间:2015-03-31 08:47:06

标签: android android-cursorloader

我已经构建了一个可以显示所有联系人的AndroidApp。 这就是我的Cursor的样子:

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只获取我的收藏夹。 我怎么能这样做?

2 个答案:

答案 0 :(得分:1)

您应该向selection添加一些CursorLoader

如果您想获取手机设置的收藏夹(例如,Android的联系人列表中的加星标联系人),则标准为ContactsContract.Contacts.STARRED

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

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

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

P.S。我没有运行上面的代码,但你应该从中掌握一般的想法。

答案 1 :(得分:1)

有一个解决方案!

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

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