Android UI(3)Getting Started - Saving Data

Android UI(3)Getting Started - Saving Data

1. Saving Data
Saving Key-Value Sets
SharedPreferences object points to a file containing key-value pairs and provides simple methods to read and write them.

Get a Handle to a SharedPreferences
getSharedPreferences(key_name,mode) - Multiple shared preference files identified by names, and this is called by Context.

In activity:
Context context = getActivity();
SharedPreferences sharedPref = context.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);

getPreferences() - Only one shared preference file for the activity.

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);

If we create preferences file with MODE_WORLD_READABLE or MODE_WORLD_WRITEABLE, then other apps can access the data.

Write to Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore); // getString()
editor.commit();

Read from Shared Preferences
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
int defaultValue = getResources().getInteger(R.string.saved_high_score_default);

long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue);

2. Saving Files
Deal with the android file system with the File API. It is good the store the image files or anything exchanged over a network.

Choose Internal or External Storage
“internal” and "external" storage are coming from the old days, when most devices offered built-in non-volatile  memory(internal storage), removable storage medium such as a micro SD card(external storage).

The apps are installed onto the internal storage by default, you can specify the android:installLocation in manifest file.

Obtain Permissions for External Storage
<manifest …>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
</manifest>

Right now, apps have the read permission by default to external storage.

And for internal storage, by default, apps have that permissions.

Save a File on Internal Storage
File handler methods:
getFileDir()

getCacheDir()

File file = new File(context.getFilesDir(), filename);

Examples to write a file>
String filename = "myfile";
String hello = "Hello";
FileOutputStream outputStream;
try{
     outputStream = openFileOutput(filename, Context.MODE_PRIVATE);
     outputStream.write(hello.getBytes());
}catch(Exception e){
     e.printStackTrace();
}finally{
     outputStream.close();
}

Save a File on External Storage
public boolean isExternalStorageWritable(){
     String state = Environment.getExternalStorageState();
     if(Evironment.MEDIA_MOUNTED.equals(state)){
          return true;
     }
     return false;
}

/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable(){
     String state = Environment.getExternalStorageState();
     if(Environment.MEDIA_MOUNTED.equals(state) ||
          Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)){
          return true;
     }
     return false;
}

Even all the files stored on external storage can be accessed by other apps. But there are still 2 parts in external, private and public.

public File getAlbumStorageDir(String albumName){
     File file = new File(Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES),albumName);
     if(!file.mkdirs()){
          Log.e(LOG_TAG, "Directory not there");
     }
     return file;
}

public File getAlbumStorageDir(Context context, String albumName){
     File file = new File(context.getExternalFilesDir(
               Environment.DIRECTORY_PICTURES), albumName);
     if(!file.mkdirs()){
          Log.e(LOG_TAG, "Directory not there!");
     }
     return file;
}

Query Free Space
getFreeSpace()
getTotalSpace()

Delete a File
myFile.delete()

myContext.deleteFile(fileName);

3. Saving Data in SQL Databases
android.database.sqlite

Define a Schema and Contract
//===========database
public static final String TABLE_NAME = "product";
public static final String COLUMN_NAME_PRODUCT_ID = "product_id";
public static final String COLUMN_NAME_PRODUCT_NAME = "product_name";
public static final String COLUMN_NAME_PRODUCT_PRICE = "product_price";
public static final String COLUMN_NAME_PRODUCT_DESN = "product_desn";
public static final String COLUMN_NAME_PRODUCT_IMAGE_URL = "product_imageurl";

publicstaticfinalStringSQL_CREATE = "CREATE TABLE " + TABLE_NAME + " ( " +
     COLUMN_NAME_PRODUCT_ID + " INTEGER PRIMARY KEY," +
     COLUMN_NAME_PRODUCT_NAME + " TEXT," +
     COLUMN_NAME_PRODUCT_PRICE + " TEXT," +
     COLUMN_NAME_PRODUCT_DESN + " TEXT," +
     COLUMN_NAME_PRODUCT_IMAGE_URL + " TEXT" +
     ");";
public static final String SQL_DROP = "DROP TABLE IF EXISTS " + TABLE_NAME;
//===========database

I defined all these things in my model object.

Create a Database Using a SQL Helper
package com.sillycat.easyrestclientandroid.dao.db;

import com.sillycat.easyrestclientandroid.model.Product;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class BasicDBHelper extends SQLiteOpenHelper {

     public static final int DATABASE_VERSION = 1;

     public static final String DATABASE_NAME = "sillycat.db";

     public BasicDBHelper(Context context) {
          super(context, DATABASE_NAME, null, DATABASE_VERSION);
     }

     public void onCreate(SQLiteDatabase db) {
          db.execSQL(Product.SQL_CREATE);
     }

     public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
          db.execSQL(Product.SQL_DROP);
          onCreate(db);
     }
   
}

Put Information into a Database
public class ProductDBDAOImpl implements ProductDAO {
     private BasicDBHelper helper;
     public ProductDBDAOImpl(BasicDBHelper helper) {
          this.helper = helper;
     }
…snip…
public Product insert(Product product) {
     synchronized (DBGuard.class) {
          SQLiteDatabase db = null;
          try {
               db = helper.getWritableDatabase();
               Long id = db.insert(Product.TABLE_NAME,
               Product.COLUMN_NAME_PRODUCT_NAME, getValues(product));
               product.setProductId(id);
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               if (db != null) {
                    db.close();
               }
          }
          return product;
     }
}


private ContentValues getValues(Product item) {
     ContentValues values = new ContentValues();
     values.put(Product.COLUMN_NAME_PRODUCT_DESN, item.getProductDesn());
     values.put(Product.COLUMN_NAME_PRODUCT_ID, item.getProductId());
     values.put(Product.COLUMN_NAME_PRODUCT_IMAGE_URL,item.getProductImageURL());
     values.put(Product.COLUMN_NAME_PRODUCT_NAME, item.getProductName());
     values.put(Product.COLUMN_NAME_PRODUCT_PRICE, item.getProductPrice());
     return values;
}


…snip…
}

Read Information from a Database
public List<Product> all() {
     List<Product> items = new ArrayList<Product>();
     synchronized (DBGuard.class) {
          SQLiteDatabase db = null;
          Cursor cursor = null;
          try {
               db = helper.getReadableDatabase();
               cursor = db.query(Product.TABLE_NAME, new String[] {
                    Product.COLUMN_NAME_PRODUCT_DESN,
                    Product.COLUMN_NAME_PRODUCT_ID,
                    Product.COLUMN_NAME_PRODUCT_IMAGE_URL,
                    Product.COLUMN_NAME_PRODUCT_NAME,
                    Product.COLUMN_NAME_PRODUCT_PRICE }, null, null, null, null, null);
               if (cursor != null && cursor.getColumnCount() > 0) {
                    cursor.moveToFirst();
                    while (cursor.getPosition() != cursor.getCount()) {
                         items.add(getItem(cursor));
                         cursor.moveToNext();
                    }
               }
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               if (cursor != null) {
                    cursor.close();
               }
               if (db != null) {
                    db.close();
               }
          }
     }
     return items;
}


private Product getItem(Cursor cursor) {
     Product item = new Product();
     item.setProductDesn(cursor.getString(0));
     item.setProductId(cursor.getLong(1));
     item.setProductImageURL(cursor.getString(2));
     item.setProductName(cursor.getString(3));
     item.setProductPrice(cursor.getString(4));
     return item;
}



Delete Information from a Database
publicboolean deleteById(Long productId) {
     synchronized (DBGuard.class) {
          SQLiteDatabase db = null;
          try {
               db = helper.getReadableDatabase();
               db.delete(Product.TABLE_NAME, "id = ?",
               new String[] { productId + "" });
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               if (db != null) {
                    db.close();
               }
          }
     }
     return false;
}


Update a Database
public Product update(Product product) {
     Product item = null;
     synchronized (DBGuard.class) {
          SQLiteDatabase db = null;
          try {
               db = helper.getWritableDatabase();
               db.update(Product.TABLE_NAME, getValues(product), "id=?",
               new String[] { "" + product.getProductId() });
          } catch (Exception e) {
               e.printStackTrace();
          } finally {
               if (db != null) {
                    db.close();
               }
          }
     }
     return item;
}



DBGuard is just an empty class that used to lock the synchronized things. I am not sure it is necessary. How can the customer type his mobile devices in that fast? Haha.


References:
http://developer.android.com/training/basics/data-storage/index.html


你可能感兴趣的:(android ui)