Object oriented design with database (java): cursors, SQLiteOpenHelper

99封情书 提交于 2019-12-25 16:54:20

问题


Currently I have a class called DBAdapter that has an inner class called DatabaseHelper which extends SQLiteOpenHelper. Without the actual implementation it would look like this (in a high level):

public class DBAdapter {
    public Cursor selectAllBooks();
    public List<Book> getAllBooks(); //This would handle the cursor for you
    private class DatabaseHelper extends SQLiteOpenHelper{}
}

This way, the calling activity could simply call dbAdapter.getAllBooks(), as opposed to having to cycle through the cursor themselves.

However, I stumbled upon a SimpleCursorAdapter, and ResourceCursorAdapter where the database (in my case DBAdapter's selectAllBooks()) would return a cursor, and then you would have a class that would handle this cursor.

http://joesapps.blogspot.com/2011/02/customized-listview-data-display-in.html

He uses this function:

private class MyListAdapter extends ResourceCursorAdapter {
    public void bindView(View view, Context context, Cursor cursor){
        ...
        int price = cursor.getInt(cursor.getColumnIndex(DbAdapter.COL_PRICE));
        ...
    }
}

Doesn't this technically not hide all implementation of the database? Here the ListAdapter needs to know of the column names etc., as opposed to my implementation which just returns a list of what they want and then the ListAdapter can do what it wants with it. I also don't need to know what column names I have to grab data from etc. If you call the function getAllBooks() you will simply get a list of books.

来源:https://stackoverflow.com/questions/21834353/object-oriented-design-with-database-java-cursors-sqliteopenhelper

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!