I am using DB browser for SQLite
to create an external database, the file name's words_library.db
inside that file I have a table called dictionary.db
that contains 3 columns
: "English_lib", "German_lib", "_id" as shown below (Fig. 1)
What I am trying to do is populating a recyclerview with that database but I failed to do so.
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.
fig 1 :
an example of the database I have and the recyclerview should only have the English_lib
and the German_lib
columns shown
to do so :
1- change the PK column name to "rowid" instead of "_id" (this will fix a lot of PK related problems not only this one)
2- update the code by adding new variables and lists like so:
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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.