我一直用SQLite编写一个简单的ExpandableListView
来填充它,但是遇到了错误,告诉我,我对游标有什么问题。这是我的代码
首先 - DBHelper。我有一个有两张桌子的小DB。首先有一个类别名称,可以有无限的子类别标题。
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);
}
}
而且,ofc,错误本身:
11-15 17:04:43.036 21404-21404 / com.example.senso.readernew E / CursorWindow:无法从CursorWindow读取第0行,第-1列 它有1行,1列。 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:进程:com.example.senso.readernew,PID:21404 11-15 17:04:43.046 21404-21404 / com.example.senso.readernew E / AndroidRuntime:java.lang.RuntimeException:无法启动活动 ComponentInfo {com.example.senso.readernew / com.example.senso.readernew.MainActivity}: java.lang.IllegalStateException:无法从中读取第0行,第1行 CursorWindow。确保之前正确初始化了Cursor 从中访问数据。 11-15 17:04:43.046 21404-21404 / com.example.senso.readernew E / AndroidRuntime:引起:java.lang.IllegalStateException: 无法从CursorWindow读取第0行col -1。确保光标 在从它访问数据之前正确初始化。 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)
提前致谢!
P.S - 我写了我的代码,从Android ExpandableListView and SQLite Database一个代码学习。