offs3t / HoboMaps

Attempt to push initial codebase to GitHub with Android Studio's integrated Git VCS
1 stars 0 forks source link

Um #11

Open offs3t opened 8 years ago

offs3t commented 8 years ago

Possibly dumb question, but related to issue of avoiding Object creation and unnecessary data structures:

Best way to return data from DB after reading from SQLite DB with checklistItemDbHelper.getReadableDatabase()?

Not sure if valid question, or dumb question

offs3t commented 8 years ago

Best way appears to be to simply pass the Cursor object around, which can be instantiated like this:

        // Select by the row ID
        String[] selectionArg = { String.valueOf(myRowID) };

        // Query the database and get a cursor
        Cursor c = db.query(
                ItemContract.ItemEntry.TABLE_NAME,
                ItemDetailActivity.PROJECTION,
                ItemDetailActivity.ROW_ID_SELECTION,    // Select by the row ID.
                selectionArg,   // Pass the row ID as the selection argument.
                null,           // Do not group the rows in the result.
                null,           // Set to null because 'groupBy' is also set to null.
                null);          // Do not sort the result, only 1 row is expected.

and read from like this:

        // Read the fields from the cursor
        c.moveToFirst();
        String existingItemName =
                c.getString(c.getColumnIndexOrThrow(ItemContract.ItemEntry.COLUMN_NAME_ITEM_NAME));
        String existingItemCategory =
                c.getString(c.getColumnIndexOrThrow(ItemContract.ItemEntry.COLUMN_NAME_CATEGORY));

It's probably the whole point of this Object, to be passed around, instead of just used at the "endpoint"

Should've read to end of method first, instead of modifying line to line, first to last

offs3t commented 8 years ago

The getExistingItem(long myRowId) method in app1 returns the values for a requested row

But in this app app2? should there be a method that returns a Cursor, which keeps track of rowIDs in itself?

Bottom of this linked section: https://developer.android.com/training/basics/data-storage/databases.html#ReadDbRow

cursor.moveToFirst();
long itemId = cursor.getLong(
    cursor.getColumnIndexOrThrow(FeedEntry._ID)
);