Here’s an example of how to save the List to Sharedpreferences:
// Constante com o nome do objeto q vamos salvar
private static final String LISTA = "LISTA";
// Constante com o nome da propriedade
private static final String ITENS = "ITENS";
private static final String SEPARADOR = ";"; // este deve ter um coringa (caracter que não exista na lista!)
/**
* Transforma a lista em uma String e salva.
* @param selecoes
* @param context
*/
public static void saveList(final List<String> selecoes, final Context context)
{
final SharedPreferences prefs = context.getSharedPreferences(LISTA, Context.MODE_PRIVATE);
final StringBuffer buffer = new StringBuffer();
for(final String s : selecoes)
{
buffer.append(s);// adicionamos o item
buffer.append(SEPARADOR);// adicionamos um separador
}
final SharedPreferences.Editor editor = prefs.edit();
editor.putString(ITENS, buffer.toString());
editor.commit(); // commitamos os dados ...
}
public static List<String> loadList(final Context context)
{
final SharedPreferences prefs = context.getSharedPreferences(LISTA, Context.MODE_PRIVATE);
final String valor = prefs.getString(ITENS, null);
if(null == valor){ // não armazenamos nada ainda...
return null;
}
final String[] lista = valor.split(SEPARADOR); /// quebramos a string em cada separador encontrado
final List<String> retorno = new ArrayList<String>(0);
for(final String s : lista)
{
if(!"".equals(s))
{
retorno.add(s); // se não for vazia, adiciona na lista
}
}
return retorno;
}
A tip:
I suggest validating whether a country is already on the list before adding
Hello ! Could you explain your problem better? Do you want to save a view? Or take the value of the String in another View?
– Thiago Luiz Domacoski
I have a view where I turned it into String using "View.toString" and saved this String using Sharedpreferences. Now I need to take this String and turn it into a view back, I don’t even know if it’s possible.
– daniel12345smith
I don’t think that’s possible! Could you tell me what is the need to record the View? So it’s easier to understand, the context, and we can help ...
– Thiago Luiz Domacoski
I have a list of countries and when I click on a country it changes color, I needed to save the items from my list that have already been clicked on and had the color changed. I’m trying to use Sharedpreferences to do this.
– daniel12345smith
This List is in Sharedpreferences???
– Thiago Luiz Domacoski
No. in Sharedpreferences I would only save the view of the line that changed color, not to save listview inside Sharedpreferences, only accepts primitive data so I was trying to convert the view to String.
– daniel12345smith
A simple suggestion: Save uam String separated by ; with the names of the selected countries!
– Thiago Luiz Domacoski
@daniel12345smith, What you are doing doesn’t seem right, when calling View.toString(), you are not saving a View in a String, you are only taking the memory address of the view. I agree with Thiago Luiz Domacoski, Voce can save a string separated by ; with the names of the selected countries.
– Julio Borges