nelok
nelok

Reputation: 11

updated listview with shared preferences

I have quiz in android app which contains list of questions user clicks on one of them and answers it and either goes to next question by clicking on next button or comes back to list questions now based on the answer given right /wrong by list question number is highlighted as green or red and grey is its default color. All this is working fine as per my choice but i want to maintain the highlighted state over different activity as well as when user exits the application.

Please guys help me as i have lapsed an official deadline just because of this ?

Upvotes: 1

Views: 91

Answers (2)

Mitesh Shah
Mitesh Shah

Reputation: 1739

Use SharedPreferences as,

To Save:

SharedPreferences settings;
SharedPreferences.Editor editor;
public static final String PREFS_NAME = "app_pref";
public static final String KEY_p_id = "KEY_test";

settings = getSharedPreferences(PREFS_NAME, 0);
editor = settings.edit();
editor.putString(Login_screen.KEY_test, values.get(0));
editor.commit();

To Remove:

editor.remove("KEY_test").commit();

Use it in your app according to your requirement.

Upvotes: 0

Carnal
Carnal

Reputation: 22064

I assume you have a class, we can call it Question, and you will have a boolean correctAnswer which you set to true/false. Now make your class Question implement Serializable so you can save these Questions in the FileSystem, or you can store them in SQLiteDatabase and don't need to implement Serializable (your choice).

Now even when application is restarted you have this data consistent in your application, hence you can load the list of questions and just check the boolean correctAnswer in order to set the right colors for each Question in your ListView.

EDIT: I just you had three states for your answers. So instead of boolean correctAnswer you can add int answered where

not answered = 0

answered wrong = 1

answered right = 2

Upvotes: 1

Related Questions