Working through the Notepad tutorial and have a question. When it builds the database with the fields rowid, title, and body - all three are private static final strings. But when a method like fetchnote() runs, it uses a long id to get the note. How does the method get that id if rowid is a string? I would like to modify the tutorial a bit and have an activity call a listactivity. The listactivity will display everything in the database, then, when a user clicks an item, I would like to copy just that entry into a new database and开发者_C百科 send the row id back to the calling activity. But I can't figure out how to get it to copy the entry into the new database because the tutorial uses a row id to make adjustments and my new database would be blank. Does that make sense? I'm new to this.
I think the public static final Strings
you're talking about are these:
public class NotesDbAdapter {
public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_ROWID = "_id";
...
}
If so, then these are the column names. The column names are strings, not the values in these columns. If you look a little bit further down you'll see:
private static final String DATABASE_CREATE =
"create table notes (_id integer primary key autoincrement, "
+ "title text not null, body text not null);";
Which is how the database is created. As you can see, the "_id" column is of type "integer", which I'm guessing is the same (or at least compatible with) long.
(Edit)
The following should enable you to copy the note out of the original table and insert it into another one.
mCursor = mDbHelper.fetchNote(rowId);
title = mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_TITLE));
body = mCursor.getString(mCursor.getColumnIndex(NotesDbAdapter.KEY_BODY));
// I'm using mDbHelper2 here to indicate that you're inserting it into
// a different table.
mDbHelper2.createNote(title, body);
// And if you want to remove it from the original table as well:
mDbHelper.deleteNote(rowId);
// I'm guessing you'd have to run this to square up all the rows in the display:
fillData();
You could just set up your new database with the same columns as the first one, but without an auto-incrementing parameter on the id column. Then you just add a new entry to it with the same details as the other one.
精彩评论