0

I'm making an app that allows me to keep track of my income/spendings in Android. I want the app to be able to show me incomes and spendings over a certain timespan.

What I've got so far is this

 public void buildArray (String num, String kat, String titel) {
        Set<String> mySet = new HashSet<String>();
        mySet.add(num);
        mySet.add(kat);
        mySet.add(titel);

        saveData(mySet);
    }


    public void saveData (Set<String> setName) {
        SharedPreferences sp = this.getActivity().getSharedPreferences("Utgifter", Context.MODE_PRIVATE);
        SharedPreferences.Editor edit = sp.edit();

        Date date = new Date();

        edit.putStringSet(String.valueOf(date), setName);
        edit.commit();


    }

For every entry the user makes I want to store four values. The amount, the category and a title for the entry, and the current time as the key.

My question is how do I read stringsets from the SharedPreferences and is using the date as a key a good idea? Is there anyway to loop through all the saved values and read the ones in a certain time interval?

Thanks!

2 Answers2

1

To me it sounds like what you need is a database.

SharedPreferences is better suited to store primitive data , once you need a bit more complex logic it will be a challenge.

You can use SQLite, and create a db manager class.

meda
  • 45,103
  • 14
  • 92
  • 122
  • Thanks for your answer. I will read up on SQLite and hopefully that solves my issue. – Martin Tocaj Oct 11 '15 at 16:21
  • Ok im sure if you look in youtube sqlite android tutorial you will find tons of ressources – meda Oct 11 '15 at 16:24
  • [H2](http://h2database.com/) is a pure Java database built to be embedded in an app. More powerful, more full-fledged database, than SQLite. See [this Question](http://stackoverflow.com/q/3346236/642706). – Basil Bourque Oct 11 '15 at 17:39
0

how do I read stringsets from the SharedPreferences
You can store set in the shared preference and to read it you can do

public Set<String> loadDataSet (String key) {
    SharedPreferences sp = this.getActivity().getSharedPreferences("Utgifter", Context.MODE_PRIVATE);
    Set<String> setName = sp.getStringSet(key, Set<String> defValues);
    return setName;
}

is using the date as a key a good idea?

what is key: The name of the preference item to retrieve and save


so if you use date as key during saving then while retrieve your data how you are going to remember the same...for that again you need to save the date somewhere. hence your key should be something independent and constant within your application

Alternativley you can go for other persistent storage mechanisms if you need to save set along with date as well.

Shadow Droid
  • 1,696
  • 1
  • 12
  • 26
  • Thanks for your reply. What other persistent storage mechanisms are you talking about? I'm currently watching some tutorials on SQLite. – Martin Tocaj Oct 11 '15 at 16:21
  • @MartinTocaj Other persistent mechanisms such as writing data into file and saving it in memory (either internal or external), using SQLite Database or, saving data on server over the network...the choice completely depends on your requirement and constraints for more info refer : http://developer.android.com/guide/topics/data/data-storage.html – Shadow Droid Oct 12 '15 at 06:58