Android - Pull data from two databases into one cursor
I made a note taking app that organizes notes by contact. I'm looking to create a ListActivity with two rows to each list item. The first row will have a basic title that the user assigns to the note. The second row I would like to place the Contacts name there. I have no idea how to even start doing this. I've been researching for the past two days, trying code out here and there, and nothing seems to come together correctly. I keep seeming to come back to MergeCursor, but I cant get it working right and I cant find any good examples out there that will help me to understand it.
My main database table is: _id, contactId, title, body, datetime.
When a note is created, the Contact ID from the built in C开发者_开发问答ontact Database is passed in to the row in my database. I'm sure this is something simple but I cant seem to get it! Any help is appreciated!
Try using the CursorJoiner. It is also not recommended to join on the Contacts _ID column as this can be changed by sync or aggregation operation. Use the LOOKUP_KEY on Contacts.
Or actually, I might have misinterpreted you question, so I'm posting another answer. It sounds like you're having a problem with the layout itself, not the technical part of storing/retrieving data.
If that is the case, I would refer you to Layout Tricks: Creating Efficient Layouts which shows a couple different things you can do.
Basically, have a piece of xml code that declares a Linear Layout
which has two child nodes, both of which are TextView
. When you go to use SimpleCursorAdapter
(which I recommend) in your ListActivity
, make the last field in your SimpleCursorAdapter
constructor "to" be new int[]{R.id.text1, R.id.text2}
which is the IDs for the TextViews
you stored in xml.
I'm referring to the SimpleCursorAdapter constructor.
Does this help?
My advice is to keep using MergeCursor and be wary that
"calls to getColumns, getColumnIndex, etc will return the value for the row that the MergeCursor is currently pointing at."
When you need a string from it do so like
String body = cursor.getString(cursor.getColumnIndex(MySqlDatabase.KEY_BODY));
This is the format you should try to use regardless of MergeCursor
or regular Cursor
精彩评论