我正在使用Loader从数据库中获取数据,而且我遇到了一些麻烦,简单来说就是这样 - 不同的是不起作用。这是我的代码:
private String[] CONTACTS_COLUMNS ={
"DISTINCT " + ContactsEntry.CONTACT_ID + " AS _id",
ContactsEntry.CONTACT_FROM,
ContactsEntry.CONTACT_NAME,
CardsPhonesEntry.CONTACT_PHONE,
CardsPhonesEntry.CARD_NUMBER
};
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(getActivity(),
ContactsEntry.CONTENT_URI,
CONTACTS_COLUMNS,
mSelection, null, null);
}
这是我的contentProvider中的一些代码:
@Nullable
@Override
public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
Cursor retCursor;
int match = sUriMatcher.match(uri);
switch (match){
case PEOPLE:
retCursor = getAllContacts(projection, selection, selectionArgs, sortOrder);
break;
default:
throw new UnsupportedOperationException("Unknown uri in query(): " + uri);
}
return retCursor;
}
private Cursor getAllContacts(String[] projection, String selection, String[] selectionArgs,
String sortOrder){
return sPeopleWithCardsAndPhones.query(
mDbHelper.getReadableDatabase(),
projection,
selection,
selectionArgs,
null, null,
sortOrder
);
}
static {
sPeopleWithCardsAndPhones = new SQLiteQueryBuilder();
sPeopleWithCardsAndPhones.setTables(
ContactsEntry.TABLE_NAME + " LEFT JOIN " + CardsPhonesEntry.TABLE_NAME + " ON " +
ContactsEntry.TABLE_NAME + "." + ContactsEntry.CONTACT_ID + " = " +
CardsPhonesEntry.TABLE_NAME + "." + CardsPhonesEntry.OWNER_ID
);
}
我做错了什么以及为什么不同意?
修改 例如:我在第一个表中有1个联系人,在第二个表中有2个通过contact_id链接到此联系人的卡;
我需要显示只有1个项目(第一次联系)的列表,而不依赖它有多少卡片。现在当初始加载器 - 它向我显示这个联系人两次,但不是一次。