1

I'm developing a quiz application with a sqlite database with using the Android Cursor. Now the user can jump between the questions. If he goes to the next question I'm calling cursor.moveToNext(), otherwise cursor.moveToPrevious. On every "jump" I fill the objects with the data of the database (Question, Answers, Picture, Category). Now I thought about creating a quiz object, which has all the required data, and put it in a HashMap. A HashMap with the Cursor position as key and the quiz object as value. So I don't have to read data from the database on every jump. In addition it would avoid multiple creation of objects.

Does it make sense?

Deno Agüero
  • 519
  • 2
  • 9
  • 27

2 Answers2

2

According to Common's Ware, Cursor buffers data into memory. This means that your app does not read directly from the database on each cursor.moveToNext(). Because of this, I don't think you need a HashMap. On the other hand, a Question or Quiz class can be very helpful in passing data around to each method that needs to use it.

Community
  • 1
  • 1
Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
  • Thank you. I already have a Question, Answer, Picture and Info object. What I don't like is that I'm always creating new objects on every jump. Therefore I thought about to put the objects in a HashMap. – Deno Agüero Sep 06 '16 at 19:36
  • @DenoAgüero I don't know if you are creating all the objects in the HashMap immediately when your app starts. If you haven't considered it, a more sophisticated approach can lazily create the objects. You do this by checking the `HashMap` if the object has been created already, then if not, create it. – Code-Apprentice Sep 06 '16 at 19:41
  • This was my question. If it makes sense to store my objects in a HashMap or would it be bad regarding of memory usage. Is this the better way compared to my current solution (creating new objects). – Deno Agüero Sep 06 '16 at 19:53
  • 1
    @DenoAgüero In situations like this, there is usually a trade off between memory usage and speed. In order to make your app faster and more responsive, you will have to use more memory. However, you can mitigate the trade off to some extent by using lazy initialization. – Code-Apprentice Sep 07 '16 at 16:44
  • Yes, you are right. My last question here: would you recommend to serialize the HashMap or should I always create a new HashMap if the user makes the quiz? – Deno Agüero Sep 07 '16 at 17:03
  • 1
    @DenoAgüero The data is already "serialized" in the database, so I don't see any obvious advantage to serialize it as a HashMap data structure. – Code-Apprentice Sep 07 '16 at 17:07
1

The Cursor doesn't make a query on each jump. It is too a container for the data, like Maps. Getting data from database and then populating a map doesn't look like a optimized solution to me. I will still like to work on the cursor and get the data.

Rachit Mishra
  • 6,101
  • 4
  • 30
  • 51