I have been codeing a simple ExpandableListView
with SQLite populating it, but stucked with an error, telling me, what i have a problem with a cursor. Here is my code
First of all - DBHelper. I have a tiny DB with two tables. First have a category name, wich can have unlimited subcategory titles.
public class DBHelper extends SQLiteOpenHelper {
private static final String DB_NAME = "main";
private static final int DB_VERSION = 1;
private SQLiteDatabase db;
public DBHelper(Context context) {
super(context, DB_NAME, null, DB_VERSION);
this.db = this.getWritableDatabase();
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(String.format("CREATE TABLE IF NOT EXISTS Category (CategoryName CHAR(50) PRIMARY KEY);"));
db.execSQL(String.format("CREATE TABLE IF NOT EXISTS SubCategory (SubCategoryName CHAR(50) PRIMARY KEY, CategoryName CHAR(50), FOREIGN KEY (CategoryName) REFERENCES Category(CategoryName));"));
db.execSQL(String.format("INSERT OR IGNORE INTO Category(CategoryName) VALUES('Games');"));
db.execSQL(String.format("INSERT OR IGNORE INTO SubCategory(SubCategoryName, CategoryName) VALUES('Arcade','Games');"));
}
public Cursor fetchGroup() {
String query = "SELECT * FROM Category";
return db.rawQuery(query, null);
}
public Cursor fetchChildren(String cat) {
String query = "SELECT * FROM SubCategory WHERE CategoryName = '" + cat + "'";
return db.rawQuery(query, null);
}
}
MainActivity.java
public class MainActivity extends Activity {
SQLiteDatabase db;
DBHelper mDbh;
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Cursor mGroupsCursor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDbh = new DBHelper(this);
mDbh.onOpen(db);
//mDbh.onCreate(db);
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
fillData();
// preparing list data
//prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
//expListView.setAdapter(listAdapter);
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
final String str = listDataChild.get(listDataHeader.get(groupPosition)).get(childPosition);
Intent i = new Intent(com.example.senso.readernew.MainActivity.this,SingleListItem.class);
i.putExtra("Name", str);
startActivity(i);
return false;
}
});
}
public class ExpandableListAdapter_DB extends SimpleCursorTreeAdapter {
public ExpandableListAdapter_DB(Cursor cursor, Context context, int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom,
int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,
childLayout, childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor)
{
Cursor childCursor = mDbh.fetchChildren(groupCursor.getString(groupCursor.getColumnIndex("Category")));
startManagingCursor(childCursor);
childCursor.moveToFirst();
return childCursor;
}
}
private void fillData()
{
mGroupsCursor = mDbh.fetchGroup();
startManagingCursor(mGroupsCursor);
mGroupsCursor.moveToFirst();
expListView = (ExpandableListView)this.findViewById(R.id.expandableListView);
ExpandableListAdapter_DB mAdapter = new ExpandableListAdapter_DB(mGroupsCursor,this,
R.layout.list_group,
R.layout.list_item,
new String[]{"CategoryName"},
new int[]{R.id.lblListHeader},
new String[]{"SubCategoryName"},
new int[]{R.id.lblListItem});
expListView.setAdapter(mAdapter);
}
}
And, ofc, the error itself:
11-15 17:04:43.036 21404-21404/com.example.senso.readernew E/CursorWindow: Failed to read row 0, column -1 from a CursorWindow which has 1 rows, 1 columns. 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: FATAL EXCEPTION: main 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: Process: com.example.senso.readernew, PID: 21404 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.senso.readernew/com.example.senso.readernew.MainActivity}: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: Caused by: java.lang.IllegalStateException: Couldn't read row 0, col -1 from CursorWindow. Make sure the Cursor is initialized correctly before accessing data from it. 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: at com.example.senso.readernew.MainActivity.fillData(MainActivity.java:162) 11-15 17:04:43.046 21404-21404/com.example.senso.readernew E/AndroidRuntime: at com.example.senso.readernew.MainActivity.onCreate(MainActivity.java:46)
Thanks in advance!
P.S - i wrote my code, learning from Android ExpandableListView and SQLite Database one code.