Back to project page Android-CriminalIntent.
The source code is released under:
MIT License
If you think the Android project Android-CriminalIntent listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.bignerdranch.android.criminalintent; //from www . ja v a 2s . c o m import android.content.Context; import android.util.Log; import org.json.JSONException; import java.io.IOException; import java.util.ArrayList; import java.util.UUID; /** * CrimeLab data model for holding an ArrayList of Crime objects * Created by mweekes on 12/19/13. */ public class CrimeLab { private static final String TAG = CrimeLab.class.getSimpleName(); private static final String FILENAME = "crimes.json"; private ArrayList<Crime> mCrimes; private CriminalIntentJSONSerializer mSerializer; private static CrimeLab sCrimeLab; private Context mAppContext; private CrimeLab(Context appContext) { mAppContext = appContext; mSerializer = new CriminalIntentJSONSerializer(mAppContext, FILENAME); try { mCrimes = mSerializer.loadCrimes(); } catch (Exception e) { mCrimes = new ArrayList<Crime>(); Log.e(TAG, "Error loading crimes: " + e); } } public void add(Crime crime) { mCrimes.add(crime); } public void delete(Crime crime) { mCrimes.remove(crime); } public boolean saveCrimes() { try { mSerializer.saveCrimes(mCrimes); Log.d(TAG, "crimes saved to file"); return true; } catch (JSONException jse) { Log.e(TAG, "Error saving crimes[JSON]: " + jse); return false; } catch (IOException ioe) { Log.e(TAG, "Error saving crimes: " + ioe); return false; } } public static CrimeLab get(Context ctx) { if (sCrimeLab == null) { sCrimeLab = new CrimeLab(ctx.getApplicationContext()); } return sCrimeLab; } public ArrayList<Crime> getCrimes() { return mCrimes; } public Crime getCrime(UUID crimeId) { for (Crime c : mCrimes) { if (c.getId().equals(crimeId)) { return c; } } return null; } }