I'm using a cursor with a custom adapter that extends SimpleCursorAdapter:
public class ListWordAdapter extends SimpleCursorAdapter {
private LayoutInflater inflater;
private Cursor mCursor;
private int mLayout;
private String[] from;
private int[] to;
public ListWordAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, int flags) {
super(context, layout, c, from, to, flags);
this.mCursor = c;
this.inflater = LayoutInflater.from(context);
this.mLayout = layout;
this.from = from;
this.to = to;
}
private static class ViewHolder {
//public ImageView img;
public TextView name;
public TextView type;
public TextView translate;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (mCursor.moveToPosition(position)) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(mLayout, null);
holder = new ViewHolder();
// holder.img = (ImageView) convertView.findViewById(R.id.img_row);
holder.name = (TextView) convertView.findViewById(to[0]);
holder.type = (TextView) convertView.findViewById(to[1]);
holder.translate = (TextView) convertView.findViewById(to[2]);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(mCursor.getString(mCursor.getColumnIndex(from[0])));
holder.type.setText(mCursor.getString(mCursor.getColumnIndex(from[1])));
holder.translate.setText(mCursor.getString(mCursor.getColumnIndex(from[2])));
// holder.img.setImageResource(img_resource);
}
return convertView;
}
}
And in the main activity I call it as:
adapter = new ListWordAdapter(getSherlockActivity(), R.layout.row_list_words, mCursorWords, from, to, 0);
When a modification in the list is made, I call this method:
public void onWordSaved() {
WordDAO wordsDao = new WordSqliteDAO();
Cursor mCursorWords = wordsDao.list(getSherlockActivity());
adapter.changeCursor(mCursorWords);
}
The thing here is that this produces me this exception:
10-29 11:14:33.810: E/AndroidRuntime(18659):
java.lang.IllegalStateException: database
/data/data/com.example.palabrasdeldia/databases/palabrasDelDia (conn#
0) already closed
Complete stack trace:
10-29 11:14:33.810: E/AndroidRuntime(18659): FATAL EXCEPTION: main
10-29 11:14:33.810: E/AndroidRuntime(18659):
java.lang.IllegalStateException: database
/data/data/com.example.palabrasdeldia/databases/palabrasDelDia (conn#
0) already closed 10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.sqlite.SQLiteDatabase.verifyDbIsOpen(SQLiteDatabase.java:2123)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:398)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.sqlite.SQLiteDatabase.lock(SQLiteDatabase.java:390)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.sqlite.SQLiteQuery.fillWindow(SQLiteQuery.java:74)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.sqlite.SQLiteCursor.fillWindow(SQLiteCursor.java:311)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.sqlite.SQLiteCursor.onMove(SQLiteCursor.java:283)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.database.AbstractCursor.moveToPosition(AbstractCursor.java:173)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
com.example.palabrasdeldia.adapters.ListWordAdapter.getView(ListWordAdapter.java:42)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.AbsListView.obtainView(AbsListView.java:2128) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.widget.ListView.makeAndAddView(ListView.java:1817) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.widget.ListView.fillSpecific(ListView.java:1361) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.widget.ListView.layoutChildren(ListView.java:1646) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.widget.AbsListView.onLayout(AbsListView.java:1979) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1527)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.LinearLayout.onLayout(LinearLayout.java:1316) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.FrameLayout.onLayout(FrameLayout.java:400) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.FrameLayout.onLayout(FrameLayout.java:400) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.LinearLayout.onLayout(LinearLayout.java:1314) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.FrameLayout.onLayout(FrameLayout.java:400) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.LinearLayout.setChildFrame(LinearLayout.java:1542)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.LinearLayout.layoutVertical(LinearLayout.java:1403)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
android.widget.LinearLayout.onLayout(LinearLayout.java:1314) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.FrameLayout.onLayout(FrameLayout.java:400) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.widget.FrameLayout.onLayout(FrameLayout.java:400) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.View.layout(View.java:9593) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewGroup.layout(ViewGroup.java:3877) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.view.ViewRoot.performTraversals(ViewRoot.java:1253) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.view.ViewRoot.handleMessage(ViewRoot.java:2017) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.os.Handler.dispatchMessage(Handler.java:99) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
android.os.Looper.loop(Looper.java:132) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
android.app.ActivityThread.main(ActivityThread.java:4028) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
java.lang.reflect.Method.invokeNative(Native Method) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
java.lang.reflect.Method.invoke(Method.java:491) 10-29 11:14:33.810:
E/AndroidRuntime(18659): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:844)
10-29 11:14:33.810: E/AndroidRuntime(18659): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 10-29
11:14:33.810: E/AndroidRuntime(18659): at
dalvik.system.NativeStart.main(Native Method)
If I use SimpleCursorAdapter directly instead of ListWordAdapter, it works fine. What's wrong with my custom adapter implementation? The line in bold in the stack trace corresponds with: if (mCursor.moveToPosition(position)) inside getView method.
EDIT:
I have created a custom class to manage DB operations as open and close:
public class ConexionBD {
private Context context;
private SQLiteDatabase database;
private DataBaseHelper dbHelper;
public ConexionBD(Context context) {
this.context = context;
}
public ConexionBD open() throws SQLException {
this.dbHelper = DataBaseHelper.getInstance(context);
this.database = dbHelper.getWritableDatabase();
database.execSQL("PRAGMA foreign_keys=ON");
return this;
}
public void close() {
if (database.isOpen() && database != null) {
dbHelper.close();
}
}
/*Getters y setters*/
public SQLiteDatabase getDatabase() {
return database;
}
public void setDatabase(SQLiteDatabase database) {
this.database = database;
}
}
And this is my DataBaseHelper:
public class DataBaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "myDb";
private static final int DATABASE_VERSION = 1;
private static DataBaseHelper sInstance = null;
public static DataBaseHelper getInstance(Context context) {
// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (sInstance == null) {
sInstance = new DataBaseHelper(context.getApplicationContext());
}
return sInstance;
}
@Override
public void onCreate(SQLiteDatabase database) {
...
}
....
And this is an example of how I manage a query:
public Cursor list(Context context) {
ConexionBD conexion = new ConexionBD(context);
Cursor mCursor = null;
try{
conexion.open();
mCursor = conexion.getDatabase().query(DataBaseHelper.TABLE_WORD , null , null, null, null, null, Word.NAME);
if (mCursor != null) {
mCursor.moveToFirst();
}
}finally{
conexion.close();
}
return mCursor;
}
For every connection to the DB I open it and close it.