Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Sauver un objet

Featured Replies

Posté(e)

Salut !

Je voudrais sauver un objet (une liste d'objets en fait)

Vous avez une idée ?

Les classes Preferences ne semblent pouvoir stocker que des types primitifs, donc ça ne va pas :(

Merci

  • 1 year later...
Posté(e)

Bonjour.

Quittes à déterrer un ancien sujet (toutes mes excuses) autant que ça serve aux gens qui se posent la question.

Si tu veux sauvegarder dans la mémoire Interne de ton téléphone il existe une méthode simple et rapide :

FileOutputStream fos = context.openFileOutput(name, Context.MODE_PRIVATE);
  ObjectOutputStream oos = new ObjectOutputStream(fos);
  oos.writeObject(dictionary);
  oos.close();

Avec "dictionary" de type :

Hashtable<String,Object> dictionary

Pour lire on pourra utiliser :

java.io.File file = context.getFileStreamPath(name);
  if(file.exists())
  {
   FileInputStream fis = context.openFileInput(name);
   ObjectInputStream ois = new ObjectInputStream(fis);
   dictionary = (Hashtable<String, Object>) ois.readObject();
   ois.close();
  }

avec name de type String (nom du fichier)

(J'ai volontairement enlevé les blocs try/catch mais ils sont essentiels pour ce type d'opérations d'entrée / sortie).

Pour plus d'informations à ce sujet je vous invite à lire cet article de la doc officielle :

http://developer.android.com/guide/topics/data/data-storage.html

Bonne chance.

  • 4 weeks later...
Posté(e)

En effet, je fais comme cela aussi.

J'ajouterais qu'il faut que les objets enregistrés soit de type Serializable, cad implements Serializable.

Et donc qu'il doivent avoir un ID unique: private static final long serialVersionUID = 1L;

Ce qui donne:

import java.io.Serializable;

public class MonObjet implements Serializable
{
 private static final long serialVersionUID = 1L;
...
}

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.