1

I am trying to get data from database to arraylist and then show them. Unfortunately I get this

error:java.lang.OutOfMemoryError: OutOfMemoryError

thrown while trying to throw an exception; no stack trace available. I am using own class for Object, which are store in database.

MainActivity code:

narodyList = db.getVsechnyNarody(); 
String k = narodyList.get(1).toString();
Toast.makeText(getApplicationContext(),"Záznam" + k,Toast.LENGTH_LONG).show();

DatabaseHandler.java:

 public List<Narody> getVsechnyNarody(){
        List<Narody> narodyList = new ArrayList<Narody>();
        String selectQuery = "SELECT  * FROM " + TABLE_NARODY;
        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        if(cursor.moveToFirst()){
            do {
                Narody narod = new Narody();
                narod.setId(Integer.parseInt(cursor.getString(0)));
                narod.setNazevNaroda(cursor.getString(1));
                narodyList.add(narod);
            } while (cursor.moveToFirst());
        }
        cursor.close();
        db.close();
        return narodyList;
    }
0

1 Answer 1

2

The mistake is probably on line 13.

This line keeps you stuck in infinite loop (you are moving to first row every time the loop is executed):

} while (cursor.moveToFirst());

Replace it with this line:

} while (cursor.moveToNext());

I hope this helped

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.