问题
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