How can I separate items from a sharedPreferences into an Array?

Asked

Viewed 53 times

1

I have a question, I made a checkbox on items in a listview that when marked are stored as Sharedpreferences. So I created a button (Favorites) to access the items marked by the checkbox, but the items marked go to Favorites I created again a new listview to receive the items, but several items comes with only one item in the Favorites listview, I would like to separate them.

Follows my code:

SharedPreferences sharedPreferences = getSharedPreferences("arquivoPreferencia", MODE_PRIVATE);

       String fraseR = String.valueOf(sharedPreferences.getAll());


            // CRIANDO O ARRAY

            final String[] frasesFavoritoArray =
                    {
                            fraseR
                    };

I believe that the problem is precisely in the Array that is receiving all the items in the 'Fraser', but I would not know to separate them.

Is there any solution to this?

Thank you very much.

1 answer

2


Trade your code for this one:

SharedPreferences sharedPreferences = getSharedPreferences("arquivoPreferencia", MODE_PRIVATE);
List<String> frasesFavoritoArray = new Arraylist<>();
Map<String, ?> allEntries = sharedPreferences.getAll();
for (Map.Entry<String, ?> entry : allEntries.entrySet()) {    
   frasesFavoritoArray.add(entry.getValue().toString());
}
  • Thank you, Marty. Solved the problem! The solution proposed above solved the problem. Had a piece of my code I had to fix, Thank you very much!!!

Browser other questions tagged

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