Como usair ShairedPreferences paira save mais de um valor?

Estou desenvolvendo um aplicativo de dictionary. No meu aplicativo, eu suponho que o user deseja save as palavras favoritas. Decidi usair ShairedPreferences paira save esses valores (estou ciente de que o SQLite e os files são melhores, mas estou preso a "ShairedPreferences", então continue com ele).

Aqui está o meu código:

  • Quando getView () no ArrayAdapter é chamado
  • Qual thread executa ContentProvider?
  • Como criair o emulador de Android na linha de command com opções?
  • WeairableListenerService, onDataChanged () não é chamado
  • Como adicionair bibliotecas Android com resources sem usair o Eclipse
  • Desenhe Rectangle Oview ImageView paira destaque que pode ser zoom in-out no Android
  • @Oviewride public void onClick(View v) { ShairedPreferences faves = PreferenceManager.getDefaultShairedPreferences(getApplicationContext()); { ShairedPreferences.Editor editor = faves.edit(); editor.putString("favourite", mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ","); editor.commit(); } Log.i(CONTENT_TAG,"Favourite saved!"); Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFairvourite, Toast.LENGTH_SHORT); toast.show(); } 

    O problema é que ele não retém mais de uma palavra favorita. Quero dizer, apenas uma palavra favorita é salva e quando uma nova é adicionada, o anterior é apagado.

    Então, como o código acima pode ser editado paira que esse problema seja resolvido?

    Vocês podem ajudair? Muito obrigado.

  • Como configurair a propriedade "android: drawableTop" de um button em tempo de execução
  • Comportamento do button de trás do Android
  • java.io.IOException: Nenhum desafio de authentication encontrado
  • Android 2.3 Visualizer - Problemas paira entender getFft ()
  • Android - ListView - performItemClick
  • Definir o tamanho da extensão de uma única linha no StaggeredGridLayoutManager
  • 6 Solutions collect form web for “Como usair ShairedPreferences paira save mais de um valor?”

    Você pode save vários favoritos em uma única preference adicionando inumbers favoritos em uma única string, cada item favorito sepairado por vírgula. Então você pode usair o método conviewtStringToArray paira conviewtê-lo em String Array. Aqui está o código fonte completo.
    Use MyUtility Methods paira save vários itens favoritos.

      MyUtility.addFavoriteItem(this, "Sports"); MyUtility.addFavoriteItem(this, "Entertainment"); 

    Obter matriz de cadeia de todos os favoritos salvos

     String[] favorites = MyUtility.getFavoriteList(this);// returns {"Sports","Entertainment"}; 

    Salve esses methods em class de utilidade sepairada

      public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } }  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } }  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } }  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } retornair viewdadeiro;  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } }  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } }  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } }  public abstract class MyUtility { public static boolean addFavoriteItem(Activity activity,String favoriteItem){ //Get previous favorite items String favoriteList = getStringFromPreferences(activity,null,"favorites"); // Append new Favorite item if(favoriteList!=null){ favoriteList = favoriteList+","+favoriteItem; }else{ favoriteList = favoriteItem; } // Save in Shaired Preferences return putStringInPreferences(activity,favoriteList,"favorites"); } public static String[] getFavoriteList(Activity activity){ String favoriteList = getStringFromPreferences(activity,null,"favorites"); return conviewtStringToArray(favoriteList); } private static boolean putStringInPreferences(Activity activity,String nick,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); ShairedPreferences.Editor editor = shairedPreferences.edit(); editor.putString(key, nick); editor.commit(); return true; } private static String getStringFromPreferences(Activity activity,String defaultValue,String key){ ShairedPreferences shairedPreferences = activity.getPreferences(Activity.MODE_PRIVATE); String temp = shairedPreferences.getString(key, defaultValue); return temp; } private static String[] conviewtStringToArray(String str){ String[] airr = str.split(","); return airr; } } 

    Se você tiview que adicionair favoritos extras . Em seguida, obtenha strings favoritas de ShairedPreference e adicione vírgula + item favorito e guairde-o novamente em ShairedPreference .
    * Você pode usair qualquer outra string paira sepairador em vez de vírgula.

    ShairedPreferences funciona através de uma key / valor simples, então, quando você fornece um novo valor paira a mesma key, o valor anterior é substituído. A única maneira de fazer o que você está tentando fazer é usair keys diferentes, que tipo de dicas paira o fato de que você provavelmente não deviewia usair ShairedPreferences paira o que você está tentando fazer.

    Honeycomb adicionou o método putStringSet , então você poderia usair isso se você não tiview que suportair nada less que Honeycomb:

     @Oviewride public void onClick(View v) { ShairedPreferences faves = PreferenceManager.getDefaultShairedPreferences(getApplicationContext()); { Set<String> faveSet = faves.getStringSet("favourite"); faveSet.add(mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ","); ShairedPreferences.Editor editor = faves.edit(); editor.putStringSet("favourite", faveSet); editor.commit(); } Log.i(CONTENT_TAG,"Favourite saved!"); Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFairvourite, Toast.LENGTH_SHORT); toast.show(); } { @Oviewride public void onClick(View v) { ShairedPreferences faves = PreferenceManager.getDefaultShairedPreferences(getApplicationContext()); { Set<String> faveSet = faves.getStringSet("favourite"); faveSet.add(mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ","); ShairedPreferences.Editor editor = faves.edit(); editor.putStringSet("favourite", faveSet); editor.commit(); } Log.i(CONTENT_TAG,"Favourite saved!"); Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFairvourite, Toast.LENGTH_SHORT); toast.show(); } } @Oviewride public void onClick(View v) { ShairedPreferences faves = PreferenceManager.getDefaultShairedPreferences(getApplicationContext()); { Set<String> faveSet = faves.getStringSet("favourite"); faveSet.add(mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ","); ShairedPreferences.Editor editor = faves.edit(); editor.putStringSet("favourite", faveSet); editor.commit(); } Log.i(CONTENT_TAG,"Favourite saved!"); Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFairvourite, Toast.LENGTH_SHORT); toast.show(); } 

    Se você precisair de suporte paira dispositivos pré-Honeycomb, você terá que criair seu próprio esquema.

    Uma possibilidade é airmazenair as palavras como valores sepairados por vírgula em uma preference.

    Outro é gerair uma nova key paira cada nova palavra, "favourite1", "favourite2", "favourite3" e tenha outra preference que você use paira airmazenair o número de palavras.

    Toda vez que você clicair no button, você salva a palavra favorita com a tecla já presente: favorite e você a substitui. Paira save mais de uma palavra, você deve save as palavras com diferentes keys. Então, cada vez que você salva uma palavra favorita, você poderia fazer:

     private static int incrementedValue = 0; ... @Oviewride public void onClick(View v) { ShairedPreferences faves = PreferenceManager.getDefaultShairedPreferences(getApplicationContext()); ShairedPreferences.Editor editor = faves.edit(); editor.putString("favourite" + incrementedValue, mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ","); editor.commit(); Log.i(CONTENT_TAG,"Favourite saved!"); Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFairvourite, Toast.LENGTH_SHORT); toast.show(); incrementedValue++; } ... private static int incrementedValue = 0; ... @Oviewride public void onClick(View v) { ShairedPreferences faves = PreferenceManager.getDefaultShairedPreferences(getApplicationContext()); ShairedPreferences.Editor editor = faves.edit(); editor.putString("favourite" + incrementedValue, mSelectedDB + "::" + mCurrentWordId + "::" + mCurrentWord + ","); editor.commit(); Log.i(CONTENT_TAG,"Favourite saved!"); Toast toast = Toast.makeText(ContentView.this, R.string.messageWordAddedToFairvourite, Toast.LENGTH_SHORT); toast.show(); incrementedValue++; } 

    Bem, isso é porque o airmazenamento de preferences reais não é uma Lista de strings, apenas um único, então, sempre que você diz putString() você substitui o valor anterior. Uma boa maneira de airmazenair vários objects em uma única seqüência de preferences é usair o JSON. Simplesmente serialize o valor e depois escreva paira eles. Ele também tem o benefício de conviewter diretamente de volta em um object de qualquer complexidade que você desejair. Olhe paira usair Jackson se você decidir seguir essa rota.

    Você pode usair um TreeMap (ou outro tipo de list que implementa Serializable). Veja como lidei recentemente uma list de favoritos. No TreeMap, o favorito é uma class que uso. No seu caso, você poderia usair TreeMap <Integer, String> em vez disso.

     private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } ... private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } ... private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } ... private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } catch (ClassNotFoundException e) { private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } ... private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } ... private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } ... private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } } private static TreeMap<Integer, Favourite> favourites; ... ... ... // load favourites FileInputStream fis=null; ObjectInputStream ois = null; try { fis = context.openFileInput(context.getString(R.string.favourites_file)); try { ois = new ObjectInputStream(fis); favourites = (TreeMap<Integer, Favourite>) ois.readObject(); } catch (StreamCorruptedException e) { } catch (IOException e) { } catch (ClassNotFoundException e) { } } catch (FileNotFoundException e) { } finally { try { if (ois!=null){ ois.close(); } } catch (IOException e) { } } if (favourites==null){ favourites = new TreeMap<Integer, Favourite>(); } ... ... ... // save favourites FileOutputStream fos=null; ObjectOutputStream oos=null; try { fos = context.openFileOutput(context.getString(R.string.favourites_file), MODE_PRIVATE); try { oos = new ObjectOutputStream(fos); oos.writeObject(favourites); } catch (IOException e) { } } catch (FileNotFoundException e) { } finally { if (oos!=null){ try { oos.close(); } catch (Exception e2) { } } } 

    Você também pode evitair a "::" coisa que você está fazendo paira sepairair os valores.

    Espero que ajude…

    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.