`

MatrixCursor和MergeCursor

 
阅读更多
1、MatrixCursor创建记录
ContentProvider对外共享数据的时候的query()方法是需要一个cursor的,
但是如果没有数据库,而项目又需要从ContentProvider读取数据的时候怎么办?
更糟糕的是其他方法操作也都是需要cursor的。
此时就需要MatrixCursor了。相当有趣,它相当于为你模拟了一个表。

 

@Override
 
public Cursor query(Uri uri, String[] projection, String selection,
   String[] selectionArgs, String sortOrder) {
  String[] tableCursor = new String[] { "name","job","salary" };
  MatrixCursor cursor = new MatrixCursor(tableCursor);
  cursor.addRow(new Object[] { "1111","1111","1111" });
  return cursor;
}
2、MergeCursor 记录的合并
  1. Cursor c0 = MailboxList.this.managedQuery(  
  2.                     EmailContent.Mailbox.CONTENT_URI,  
  3.                     MailboxList.this.mListAdapter.PROJECTION,  
  4.                     MAILBOX_SELECTION0,  
  5.                     new String[] { String.valueOf(mAccountKey) },  
  6.                     MailboxColumns.TYPE);  
  7.             Cursor c1 = MailboxList.this.managedQuery(  
  8.                         EmailContent.Mailbox.CONTENT_URI,  
  9.                         MailboxList.this.mListAdapter.PROJECTION,  
  10.                         MAILBOX_SELECTION,  
  11.                         new String[] { String.valueOf(mAccountKey) },  
  12.                         MailboxColumns.TYPE/* + "," + MailboxColumns.DISPLAY_NAME*/);  
  13.             Cursor c2 = MailboxList.this.managedQuery(  
  14.                     EmailContent.Mailbox.CONTENT_URI,  
  15.                     MailboxList.this.mListAdapter.PROJECTION,  
  16.                     MAILBOX_SELECTION1,  
  17.                     new String[] { String.valueOf(mAccountKey) },  
  18.                     MailboxColumns.TYPE/* + "," + MailboxColumns.DISPLAY_NAME*/);  
  19.                  
  20.             Cursor[] cursors = new Cursor[] { c0, c1, c2 };  
  21.             Cursor c = new MergeCursor(cursors);  
Cursor c0 = MailboxList.this.managedQuery(
                    EmailContent.Mailbox.CONTENT_URI,
                    MailboxList.this.mListAdapter.PROJECTION,
                    MAILBOX_SELECTION0,
                    new String[] { String.valueOf(mAccountKey) },
                    MailboxColumns.TYPE);
        	Cursor c1 = MailboxList.this.managedQuery(
                        EmailContent.Mailbox.CONTENT_URI,
                        MailboxList.this.mListAdapter.PROJECTION,
                        MAILBOX_SELECTION,
                        new String[] { String.valueOf(mAccountKey) },
                        MailboxColumns.TYPE/* + "," + MailboxColumns.DISPLAY_NAME*/);
        	Cursor c2 = MailboxList.this.managedQuery(
                    EmailContent.Mailbox.CONTENT_URI,
                    MailboxList.this.mListAdapter.PROJECTION,
                    MAILBOX_SELECTION1,
                    new String[] { String.valueOf(mAccountKey) },
                    MailboxColumns.TYPE/* + "," + MailboxColumns.DISPLAY_NAME*/);
               
            Cursor[] cursors = new Cursor[] { c0, c1, c2 };
            Cursor c = new MergeCursor(cursors);


分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics