0

In my app,when the user first get in , he can add two dates and the app makes a days calculation.Then,this result writes to a database and also prints in a textView in my main Activity(called arxiki.class).I would like the app to remember this result and print it every time the user goes to the main activity.

As i have make it,every time that the user changes activity or close the app,the next time the app ask him again to enter dates and creates a new data in my database.How can i get always the first result?thanks

my code for saving the date result is :

DbXronia entry_add = new DbXronia(arxiki.this);
            entry_add.open();
            entry_add.createEntry_xronia(meres);
            entry_add.close();

2 Answers 2

1

As i have make it,every time that the user changes activity or close the app,the next time the app ask him again to enter dates and creates a new data in my database.How can i get always the first result?thanks

So you want to ask user for dates only once(only first time when app is lauchned). This "algorithm" shouldn't be tricky. You need some mechanism when user enter dates and values are written into database that save state "dates are stored".

You can use to reach your goal SharedPreferences. Once values are written into database, you save "this action" into SharedPreferences and then when user launch app next time, just create condition that will wrap your action.

Pseudocode:

// saving action
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prefs.edit().putBoolean("isInserted", true);

// retrieve value
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
boolean isInserted = prefs.getBoolean("isInserted", false);

if (!isInserted) {
   // user can add dates
}
else {
  // do your stuff
}
Sign up to request clarification or add additional context in comments.

Comments

0

What kind of database do you use at the backend - SQLite?

If you are using SQLite you can just order the data you fetch from the database by the autoincremented *_id* coloumn. As an alternative you could at some kind of timestamp which you store into the database when you insert the new entry. Query would look something like:

Cursor c = db.query (true, table, new String[]{dataColumn}, null, null, null, null, "_id DESC", "LIMIT 1")
if (c.moveToFirst()) {
   String result = c.getString(0);
}
c.close();

2 Comments

yes i use a sqlite db.this code will be written in my main class or in the db creator class?
Guess you should add some kind of getEntry() method to your database class where you call the code. Just post your database class and I'll show you how to extend it.

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.