how to show data from a sharedPreferences in a Listactivity

Asked

Viewed 1,124 times

1

I’m starting to work with Sharedpreferences on android, a simple and fast way to store static and primitive data. However, my problem is showing these values stored in a Listactivity. I’ve looked here on the forum, tutorials on google, but no method worked. If anyone can help me thank you because I need to deliver this work this week.

3 answers

1

To get all stored values use the method getAll() of Sharedpreferences:

Map<String,?> map = prefs.getAll();

For a list of values use:

List<Value> list = new ArrayList<Value>(map.values());

Use the list to build the Listview as usual.

If you want to get the list from Keys use:

List<Value> list = new ArrayList<Value>(map.keys());

Android provides the api Preferences to program an interface of the "system settings".
Instead of using objects View to create the user interface, the settings are created through several subclasses of the class Preference declared in an XML file.

To display the preferences list is used a Preferenceactivity(API < 10) or Preferencefragment(API >= 10).

To documentation has an article in which explains in detail how to do it.

0

Putting values:

public static final String PREFS = "MinhaPreferencia";
SharedPreferences.Editor editor = getSharedPreferences(PREFS, MODE_PRIVATE).edit();
editor.putString("nomeUsuario", "Marllon Nasser");
editor.putInt("idUsuario", 10);
editor.commit();

Getting values:

SharedPreferences prefs = getSharedPreferences(PREFS, MODE_PRIVATE); 
String textoArmazenado = prefs.getString("text", null);
if (textoArmazenado != null) {
   String nomeUsuario= prefs.getString("nomeUsuario", "Nenhum nome definido");//"Nenhum nome definido" é o valor "default"
   int idUsuario = prefs.getInt("idUsuario", 0); //0 é o valor "default"
}

More information here and here

  • This I did and I’m using the log to check the received data, and everything ok. My problem is in Listactivity when I use Simpleadapter to display values in listview.

  • Can you edit your question with the snippet of the code in question? SharedPreferences, the snippet of you getting these values and the snippet of you trying to put in the Adapter

  • Could you show the data structure you’re working on? What are you saving !

  • Thiago, SharedPreferences is based on keeping small information, identified by a "Key". That is, I keep the following: Chave: Valor. I mean, I keep a certain value given a reference name. ReferenciaX: ValorXPTO

  • Yes, I know that, but how are you willing, these values? Look at my answer and see if it helps you! Hugs!

0

Here’s an example of how to catch a Set<String> :

  // Pegamos a lista do SharedPreferences
        Set<String> itens = sharedPreferences.getStringSet(ITENS_LISTA, null);
        if(null == itens){
            // Se for nula, vamos popular..
            itens = new HashSet<>(0);
            int pt = 1;
            while(pt < 10){
                itens.add("Item "+pt);
                pt++;
            }
            // Editor , para atualizar a lista
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putStringSet(ITENS_LISTA, itens);
        }


        // criamos uma lista de String
        String[] valores = new String[itens.size()];
        int pt=0;
        for(String item : itens){
            valores[pt] = item;
            pt++;
        }

        // Instancia do ArrayAdapter
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,  android.R.layout.simple_list_item_1, valores);

Browser other questions tagged

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