SharedPreferences within each ListView item

1

Let's go to the example below:

I have two items in my ListView :

Item 1

Item 2

Within each item has a calculator that stores the result. Being that:

Item 1 = 0

Item 2 = 0

In the calculator that is in item 1, I add 20 or any other number that I want to add, and then item 1 will store the result for min, with SharedPreferences .

Now how do I want item 1 and item 2 to be:

Item 1 = 20

Item 2 = 0

But in reality they look like this:

Item 1 = 20

Item 2 = 20

In short, I just added% to Item 1 , 20 is with Item 2 also being that I did not add anything to 20 only Item 2 , I wanted it when I add the value in% with% of it to keep, and the other values would remain 0.

That's what I hope you can understand.

    
asked by anonymous 05.12.2014 / 22:17

1 answer

2

Gustavo,

You need to save each item with a different key. Example:

SharedPreferences  save = getSharedPreferences("save",Context.MODE_PRIVATE);
SharedPreferences.Editor editor = save.edit();
editor.putFloat("valor1", valor1);
editor.putFloat("valor2", valor2);
editor.commit();

What is happening is that you are using the same key (key) to save the items, this will replace the last saved value.

    
09.12.2014 / 12:06