0

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

enter image description here

10
  • provide your LogCat Commented Jun 13, 2017 at 4:01
  • try Cursor cursor = sd.rawQuery("SELECT * FROM dictionary_lib", null); and see what happens. Commented Jun 13, 2017 at 4:50
  • @Mehran Zamani I still get the same error (check my question I posted the Log) Commented Jun 13, 2017 at 4:56
  • it isn't clear that error's belongs to which line. Commented Jun 13, 2017 at 5:01
  • @MehranZamani the Log shows that it belong to this line namelist.put(cursor.getString(ii), cursor.getString(cursor.getColumnIndex("English_lib"))); Commented Jun 13, 2017 at 5:08

1 Answer 1

1

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);
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.