views:

46

answers:

2

I want to store a few values in the form of high scores. But since im not going to be storing more than 5 values, using SQLlite doesnt seem appropriate. Another option considering was a flat file, but not sure how to go about..

+2  A: 

See here for your Data Storage options. I suppose that in your case the easiest will be to use SharedPreferences.

You could also use Internal Storage to save data in a file that is private to your application. I wouldn't recommend to use External Storage for storing high scores.

kgiannakakis
+2  A: 

If it's an array you can use this:

    public void saveArray(String filename, String[] output_field) {
         try {
            FileOutputStream fos = new FileOutputStream(filename);
            GZIPOutputStream gzos = new GZIPOutputStream(fos);
            ObjectOutputStream out = new ObjectOutputStream(gzos);
            out.writeObject(output_field);
            out.flush();
            out.close();
         }
         catch (IOException e) {
             e.getStackTrace();
         }
      }

      @SuppressWarnings("unchecked")
    public String[] loadArray(String filename) {
          try {
            FileInputStream fis = new FileInputStream(filename);
            GZIPInputStream gzis = new GZIPInputStream(fis);
            ObjectInputStream in = new ObjectInputStream(gzis);
            String[] read_field = (String[])in.readObject();
            in.close();
            return read_field;
          }
          catch (Exception e) {
              e.getStackTrace();
          }
          return null;
      }

You just call it like this:

Save Array: saveArray("/sdcard/.mydata/data.dat", MyArray);
Load Array: String[] MyArray = loadArray("/sdcard/.mydata/data.dat");

You can see an example at http://androidworkz.com/2010/07/06/source-code-imageview-flipper-sd-card-scanner/

androidworkz