在Android中使用游标表联盟 [英] Union of Tables with Cursor in Android
问题描述
我试图联合与同一领域的两个表创建我使用创建我的ListView单个光标(通过内容提供商)。
@覆盖
公共光标查询(URI URI,字符串[]投影,字符串的选择,
的String [] selectionArgs两个,字符串中将sortOrder){ SQLiteQueryBuilder QB =新SQLiteQueryBuilder();
字符串GROUPBY = NULL; 开关(sUriMatcher.match(URI)){ 案例清单:
StringBuilder的SB =新的StringBuilder(); 对于(一个String:投影)
sb.append(多个).append(,); 串projectionStr = sb.toString();
projectionStr = projectionStr.substring(0,
projectionStr.length() - 1); 的String [] =子查询新的String [] {
SELECT+ projectionStr +FROM+ Customer.TABLE_NAME,
SELECT+ projectionStr +FROM
+ IndividualCustomer.TABLE_NAME};
SQL字符串= qb.buildUnionQuery(子查询中将sortOrder,NULL);
SQLiteDatabase分贝= mDatabaseHelper.getReadableDatabase();
光标mCursor = db.rawQuery(SQL,NULL); mCursor.setNotificationUri(的getContext()getContentResolver(),URI); 返回mCursor;
即使两个表是空的,我得到两个空行,这在我的列表视图创建两行。我该如何摆脱这个问题的?
此外,当我从ListView中删除一行,光标没有得到尽管setNotificationUri的更新()
任何指针,将AP最多preciated
解决 - 我不得不子句中的列之一,提供一组(投影)有一个TOTAL(...)功能<。 / p>
I am trying to UNION two tables with the same fields to create a single cursor (through a content provider) that I am using to create my ListView.
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
String groupBy = null;
switch (sUriMatcher.match(uri)) {
case LIST:
StringBuilder sb = new StringBuilder();
for (String s : projection)
sb.append(s).append(",");
String projectionStr = sb.toString();
projectionStr = projectionStr.substring(0,
projectionStr.length() - 1);
String[] subQueries = new String[] {
"SELECT " + projectionStr + " FROM " + Customer.TABLE_NAME,
"SELECT " + projectionStr + " FROM "
+ IndividualCustomer.TABLE_NAME };
String sql = qb.buildUnionQuery(subQueries, sortOrder, null);
SQLiteDatabase db = mDatabaseHelper.getReadableDatabase();
Cursor mCursor = db.rawQuery(sql, null);
mCursor.setNotificationUri(getContext().getContentResolver(), uri);
return mCursor;
Even if the two tables are empty, I get two null rows, which creates two rows in my listview. How do I get rid of this problem?
Additionally, when I delete a row from the ListView, the cursor is not getting updated in spite of setNotificationUri()
Any pointers, will be most appreciated
Solved - I had to supply a group by clause as one of the columns (of the projection) had a "TOTAL(...)" function.
这篇关于在Android中使用游标表联盟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!