private void saveInFile() { try { FileOutputStream fos = openFileOutput(FILENAME, 0); BufferedWriter out = new BufferedWriter(new OutputStreamWriter(fos)); Gson gson = new Gson(); gson.toJson(PurchLog.getFuelPurchases(), out); out.flush(); fos.close(); } catch (FileNotFoundException e) { throw new RuntimeException(); } catch (IOException e) { throw new RuntimeException(); } }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_list_purchases); loadFromFile(); adapter = new ArrayAdapter<FuelPurchase>(this, R.layout.list_view, PurchLog.getFuelPurchases()); adapter.notifyDataSetChanged(); Button clearButton = (Button) findViewById(R.id.clear); clearButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { setResult(RESULT_OK); PurchLog.clear_log(); saveInFile(); } }); Button newButton = (Button) findViewById(R.id.new_purch); newButton.setOnClickListener( new View.OnClickListener() { public void onClick(View v) { setResult(RESULT_OK); Intent intent = new Intent(ListPurchases.this, PurchaseEntry.class); Gson gson = new Gson(); String fuel_purchase_list = gson.toJson(PurchLog.getFuelPurchases()); intent.putExtra("list_as_string", fuel_purchase_list); startActivity(intent); finish(); } }); }