【发布时间】:2017-06-13 03:38:24
【问题描述】:
我正在使用DB browser for SQLite 创建一个外部数据库,该文件中的文件名words_library.db 我有一个名为dictionary.db 的表,其中包含3 columns:“English_lib”、“German_lib”、“_id”为如下图(图1)
我正在尝试使用该数据库填充回收视图,但我没有这样做。
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);
}
日志
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 列
【问题讨论】:
-
提供您的 LogCat
-
试试
Cursor cursor = sd.rawQuery("SELECT * FROM dictionary_lib", null);看看会发生什么。 -
@Mehran Zamani 我仍然遇到同样的错误(检查我发布日志的问题)
-
不清楚错误属于哪一行。
-
@MehranZamani 日志显示它属于这一行
namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("English_lib")));
标签: java android mysql database sqlite