package com.painLogger; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; //import java.util.Map; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.content.SharedPreferences; //import android.graphics.Color; import android.graphics.Typeface; import android.os.Bundle; //import android.view.LayoutInflater; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; //import android.view.ViewGroup; import android.view.View.OnClickListener; //import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.AdapterView.OnItemLongClickListener; import android.widget.Button; import android.widget.ListView; import android.widget.SimpleAdapter; import android.widget.TextView; import android.widget.Toast; public class AddScreen extends Activity implements OnClickListener, OnItemClickListener, OnItemLongClickListener { /** Called when the activity is first created. */ Button addButton; SimpleAdapter adapter; List> painItems = new ArrayList>(); ListView listthings; int[] to; String[] from; String painLevelString, timeOfPainString, textTreatmentString, painLocation, row1, row2, name; TextView painTitle, rw1, rw2; boolean myFirstTime; String serfilename; boolean isNull = true; @Override public void onCreate(Bundle savedInstanceState) { Toast.makeText(this, "To Delete an Item, Press and Hold", Toast.LENGTH_SHORT).show(); super.onCreate(savedInstanceState); setContentView(R.layout.addscreen); // getPainItems from the saved file serfilename = "hello"; if (loadListFromFile((ArrayList>) painItems) != null) painItems = loadListFromFile((ArrayList>) painItems); SharedPreferences settings = this.getSharedPreferences("MyApp", 0); SharedPreferences.Editor e = settings.edit(); boolean firstrun = settings.getBoolean("firstrun", true); if (firstrun) { e.putBoolean("firstrun", false); e.commit(); Intent intent = new Intent(this, AddMyInfo.class); startActivity(intent); } if (this.getIntent().hasExtra("name")) { name = getIntent().getStringExtra("name"); e.putString("name", name); e.commit(); } if (getIntent().getStringExtra("name") == null) name = settings.getString("name", "None"); // initialize painTitle and set its font painTitle = (TextView) findViewById(R.id.painTitle); painTitle.setText(name + "'s Pain Log"); Typeface font = Typeface.createFromAsset(getAssets(), "Chantelli_Antiqua.ttf"); painTitle.setTypeface(font); listthings = (ListView) findViewById(R.id.listthings); from = new String[] { "row_1", "row_2" }; to = new int[] { R.id.row1, R.id.row2 }; adapter = new Adapter(this, painItems, R.layout.mylistlayout, from, to); listthings.setAdapter(adapter); listthings.setOnItemClickListener(this); listthings.setOnItemLongClickListener(this); listthings.setFastScrollEnabled(true); addButton = (Button) findViewById(R.id.addButton); addButton.setOnClickListener(this); if (getIntent().getStringExtra("newPainLevel") != null) { createNewEditedEntry(); } } public class Adapter extends SimpleAdapter { HashMap map = new HashMap(); View row; public Adapter(Context context, List> data, int resource, String[] from, int[] to) { super(context, data, resource, from, to); } @Override public View getView(int position, View convertView, ViewGroup parent) { row = convertView; row = super.getView(position, convertView, parent); if (row == null) { LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = mInflater.inflate(R.layout.mylistlayout, parent, false); } if (Integer.valueOf(painItems.get(position).get("row_3")) != null) { if (Integer.valueOf(painItems.get(position).get("row_3")) == 2) { row.setBackgroundColor(0xFF0000FF); Toast.makeText(getApplicationContext(), "this one", Toast.LENGTH_SHORT).show(); } } return row; } } private void createNewEditedEntry() { String newPainLevel = this.getIntent().getStringExtra("newPainLevel"); String newPainTime = this.getIntent().getStringExtra("newPainTime"); String newTreatment = this.getIntent().getStringExtra("newTreatment"); painLevelString = newPainLevel; timeOfPainString = newPainTime; textTreatmentString = newTreatment; painLocation = this.getIntent().getStringExtra("painLocation"); row1 = this.getIntent().getStringExtra("painLocation"); row2 = this.getIntent().getStringExtra("painDate"); // Toast.makeText(this, getIntent().getStringExtra("position"), // Toast.LENGTH_SHORT).show(); int place = Integer.valueOf(getIntent().getStringExtra("position")); painItems.remove(place); adapter.notifyDataSetChanged(); addItem(); } // private String getPainLevelString(){ // return painLevelString; // } @Override // on the activityresult,get the string extra, then add the item to the list protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == 1) { row1 = data.getStringExtra("com.painLogger.row1"); row2 = data.getStringExtra("com.painLogger.row2"); painLevelString = data.getStringExtra("com.painLogger.painLevel"); painLocation = data.getStringExtra("painLocation"); timeOfPainString = data.getStringExtra("com.painLogger.painTime"); textTreatmentString = data .getStringExtra("com.painLogger.treatment"); addItem(); } } // to add the item, put it in the map, and add the map into the list private void addItem() { HashMap map = new HashMap(); map.put("row_1", row1); map.put("row_2", row2); map.put("row_3", painLevelString); map.put("row_4", painLocation); map.put("row_5", timeOfPainString); map.put("row_6", textTreatmentString); painItems.add(map); adapter.notifyDataSetChanged(); } public void onClick(View v) { // When '+' clicked, go to the PainLoggerActivity.java, where you can // click enter, and that sends those strings back to here, where I can // incorporate them into a list view, same as was there in the // PainLogger Activity if (v == this.addButton) { Intent goToFields = new Intent(this, PainLoggerActivity.class); // put the desired extras into the intent startActivityForResult(goToFields, 1); } } public void onItemClick(AdapterView a, View v, int position, long id) { Intent intent = new Intent(this, Item1.class); intent.putExtra("painDate", painItems.get(position).get("row_2")); intent.putExtra("com.painLogger.painLevel", painItems.get(position) .get("row_3")); intent.putExtra("com.painLogger.painTime", painItems.get(position).get("row_5")); intent.putExtra("com.painLogger.treatment", painItems.get(position) .get("row_6")); intent.putExtra("painLocation", painItems.get(position).get("row_4")); intent.putExtra("position", String.valueOf(position)); startActivity(intent); } @SuppressWarnings("unchecked") private ArrayList> loadListFromFile( ArrayList> masterlistrev) { try { FileInputStream fis = openFileInput(serfilename); ObjectInputStream ois = new ObjectInputStream(fis); masterlistrev = (ArrayList>) ois .readObject(); ois.close(); } catch (Exception e) { e.printStackTrace(); } return masterlistrev; } /* * private void writeListToFile( ArrayList> * masterlistrev, Context ctx, String filename) { * * FileOutputStream fos; try { fos = ctx.openFileOutput(filename, * Context.MODE_PRIVATE); ObjectOutputStream oos = new * ObjectOutputStream(fos); oos.writeObject(masterlistrev); oos.close(); } * catch (FileNotFoundException e) { e.printStackTrace(); } catch * (IOException e) { e.printStackTrace(); } * * } */ private void writeListToFile( ArrayList> masterlistrev, Context ctx, String filename) { FileOutputStream fos; ObjectOutputStream oos = null; try { fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE); oos = new ObjectOutputStream(fos); oos.writeObject(masterlistrev); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { try { oos.close(); } catch (IOException e) { e.printStackTrace(); } } } protected void onStop() { super.onStop(); writeListToFile((ArrayList>) painItems, getApplicationContext(), serfilename); } protected void onDestroy() { super.onDestroy(); writeListToFile((ArrayList>) painItems, getApplicationContext(), serfilename); } @Override public boolean onItemLongClick(AdapterView arg0, View arg1, final int position, long id) { AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Do you want to delete this Item?") .setCancelable(false) .setPositiveButton("Yes", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { painItems.remove(position); adapter.notifyDataSetChanged(); } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { dialog.cancel(); } }); AlertDialog alert = builder.create(); alert.show(); return false; } }