How to pass the contents of a String (which I took from View.toString() to a View?

Asked

Viewed 91 times

2

I used "View.toString" to take the contents of a string and save in Sharedpreferences, since I need to save the view inside and Sharedpreferences only accepts primitive type, but now I need to set this String in a view that I believed. Is it possible to do this? I know you must have some specific method but I’m not finding out which one. I thank you already!

  • Hello ! Could you explain your problem better? Do you want to save a view? Or take the value of the String in another View?

  • 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.

  • 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 ...

  • 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.

  • This List is in Sharedpreferences???

  • 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.

  • A simple suggestion: Save uam String separated by ; with the names of the selected countries!

  • 1

    @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.

Show 3 more comments

1 answer

1

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

Browser other questions tagged

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