Sharedpreferences within each Listview item

Asked

Viewed 124 times

1

Let’s get to the point:

I have two items in my ListView:

Item 1

Item 2

Inside each item has a calculator that stores the result. Whereas:

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 the SharedPreferences.

Now how I want you to item 1 and the item 2 are:

Item 1 = 20

Item 2 = 0

But in reality they stay that way:

Item 1 = 20

Item 2 = 20

In short: I added just to the Item 1 the value 20, the Item 2 is with 20 also being that I have added nothing to the Item 2 only to the Item 1, I wanted when I added the value in Item 1 he kept, and the other values continued 0.

That’s what I hope you can understand.

1 answer

2

Gustavo,

you need to save each item with a different key (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, it replaces the last saved value.

  • More like I will create a different key being that I do not know which items in the list the user will create ?

  • The system is the following I have that list there that is created with SQL, and within the click of the list item I want to do that more I don’t know how to make each different key within the item.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.