我正在使用DB browser for SQLite
创建一个外部数据库,该文件中的文件名为words_library.db
我有一个名为dictionary.db
的表,其中包含3 columns
: " English_lib"," German_lib"," _id"如下图所示(图1)
我要做的是用该数据库填充Recyclerview,但我没有这样做。
JAVA
public void fetchData() //this method is called to populate the RecyclerView
{
db =new DataBaseHelper(getContext());
try {
db.createDataBase();
db.openDataBase();
}
catch (Exception e)
{
e.printStackTrace();
}
namelist=new LinkedHashMap<>();
int ii;
SQLiteDatabase sd = db.getReadableDatabase();
Cursor cursor = sd.query("dictionary_lib" ,null, null, null, null, null, null);
ii=cursor.getColumnIndex("_id");
eng_list=new ArrayList<String>();
ger_list= new ArrayList<String>();
id_list= new ArrayList<String>();
cursor.moveToFirst();
while (cursor.moveToNext()){
namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("English_lib")));
}
Iterator entries = namelist.entrySet().iterator();
while (entries.hasNext()) {
Map.Entry thisEntry = (Map.Entry) entries.next();
id_list.add(String.valueOf(thisEntry.getKey()));
eng_list.add(String.valueOf(thisEntry.getValue()));
ger_list.add(String.valueOf(thisEntry.getValue()));
}
for (int i = 0; i < eng_list.size(); i++) {
data.add(new DictObjectModel(eng_list.get(i), ger_list.get(i)));
}
adapter = new CustomAdapter(data);
rv.setAdapter(adapter);
}
LOG
Failed to read row 1, column -1 from a CursorWindow which has 3469 rows, 2 columns.
java.lang.IllegalStateException: Couldn't read row 1, col -1 from CursorWindow.
Make sure the Cursor is initialized correctly before accessing data from it.
图1:
我拥有的数据库示例和recyclerview应该只显示English_lib
和German_lib
列
答案 0 :(得分:1)
这样做:
1 - 将PK列名称更改为“rowid”而不是“_id”(这将解决很多PK相关问题,而不仅仅是这一个)
2 - 通过添加新变量和列表来更新代码:
public void fetchData()
{
db = new DataBaseHelper(getContext());
try {
db.createDataBase();
db.openDataBase();
}
catch (Exception e)
{
e.printStackTrace();
}
namelist = new LinkedHashMap<>();
nmlist = new LinkedHashMap<>();
int ii;
SQLiteDatabase sd = db.getReadableDatabase();
Cursor cursor = sd.query("dictionary_lib" ,new String[]{
"English_lib", "German_lib", "rowid"
}, null, null, null, null, null);
ii=cursor.getColumnIndexOrThrow("rowid");
eng_list = new ArrayList<String>();
ger_list = new ArrayList<String>();
id_list = new ArrayList<String>();
while (cursor.moveToNext()){
namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndexOrThrow("English_lib")));
nmlist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndexOrThrow("German_lib")));
}
Iterator entries = namelist.entrySet().iterator();
Iterator entrs = nmlist.entrySet().iterator();
while (entries.hasNext() && entrs.hasNext()) {
Map.Entry thisEntry = (Map.Entry) entries.next();
Map.Entry altEntry = (Map.Entry) entrs.next();
id_list.add(String.valueOf(thisEntry.getKey()));
eng_list.add(String.valueOf(thisEntry.getValue()));
ger_list.add(String.valueOf(altEntry.getValue()));
}
for (int i = 0; i < id_list.size(); i++) {
data.add(new DictObjectModel(eng_list.get(i), ger_list.get(i)));
}
adapter = new CustomAdapter(data);
rv.setAdapter(adapter);
}