Advertisement
Guest User

Activity

a guest
Aug 3rd, 2011
717
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.09 KB | None | 0 0
  1. package com.painLogger;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.ObjectInputStream;
  8. import java.io.ObjectOutputStream;
  9. import java.util.ArrayList;
  10. import java.util.HashMap;
  11. import java.util.List;
  12. import java.util.Map;
  13. //import java.util.Map;
  14.  
  15. import android.app.Activity;
  16. import android.app.AlertDialog;
  17. import android.content.Context;
  18. import android.content.DialogInterface;
  19. import android.content.Intent;
  20. import android.content.SharedPreferences;
  21. //import android.graphics.Color;
  22. import android.graphics.Typeface;
  23. import android.os.Bundle;
  24. //import android.view.LayoutInflater;
  25. import android.view.LayoutInflater;
  26. import android.view.View;
  27. import android.view.ViewGroup;
  28. //import android.view.ViewGroup;
  29. import android.view.View.OnClickListener;
  30. //import android.view.ViewGroup;
  31. import android.widget.AdapterView;
  32. import android.widget.AdapterView.OnItemClickListener;
  33. import android.widget.AdapterView.OnItemLongClickListener;
  34. import android.widget.Button;
  35. import android.widget.ListView;
  36. import android.widget.SimpleAdapter;
  37. import android.widget.TextView;
  38. import android.widget.Toast;
  39.  
  40. public class AddScreen extends Activity implements OnClickListener,
  41.         OnItemClickListener, OnItemLongClickListener {
  42.     /** Called when the activity is first created. */
  43.     Button addButton;
  44.     SimpleAdapter adapter;
  45.     List<HashMap<String, String>> painItems = new ArrayList<HashMap<String, String>>();
  46.     ListView listthings;
  47.     int[] to;
  48.     String[] from;
  49.     String painLevelString, timeOfPainString, textTreatmentString,
  50.             painLocation, row1, row2, name;
  51.     TextView painTitle, rw1, rw2;
  52.     boolean myFirstTime;
  53.  
  54.     String serfilename;
  55.     boolean isNull = true;
  56.  
  57.     @Override
  58.     public void onCreate(Bundle savedInstanceState) {
  59.         Toast.makeText(this, "To Delete an Item, Press and Hold",
  60.                 Toast.LENGTH_SHORT).show();
  61.         super.onCreate(savedInstanceState);
  62.         setContentView(R.layout.addscreen);
  63.         // getPainItems from the saved file
  64.         serfilename = "hello";
  65.         if (loadListFromFile((ArrayList<HashMap<String, String>>) painItems) != null)
  66.             painItems = loadListFromFile((ArrayList<HashMap<String, String>>) painItems);
  67.  
  68.         SharedPreferences settings = this.getSharedPreferences("MyApp", 0);
  69.         SharedPreferences.Editor e = settings.edit();
  70.  
  71.         boolean firstrun = settings.getBoolean("firstrun", true);
  72.         if (firstrun) {
  73.             e.putBoolean("firstrun", false);
  74.             e.commit();
  75.             Intent intent = new Intent(this, AddMyInfo.class);
  76.             startActivity(intent);
  77.         }
  78.  
  79.         if (this.getIntent().hasExtra("name")) {
  80.             name = getIntent().getStringExtra("name");
  81.             e.putString("name", name);
  82.             e.commit();
  83.         }
  84.         if (getIntent().getStringExtra("name") == null)
  85.             name = settings.getString("name", "None");
  86.  
  87.         // initialize painTitle and set its font
  88.         painTitle = (TextView) findViewById(R.id.painTitle);
  89.         painTitle.setText(name + "'s Pain Log");
  90.         Typeface font = Typeface.createFromAsset(getAssets(),
  91.                 "Chantelli_Antiqua.ttf");
  92.         painTitle.setTypeface(font);
  93.         listthings = (ListView) findViewById(R.id.listthings);
  94.         from = new String[] { "row_1", "row_2" };
  95.         to = new int[] { R.id.row1, R.id.row2 };
  96.  
  97.         adapter = new Adapter(this, painItems, R.layout.mylistlayout, from, to);
  98.  
  99.         listthings.setAdapter(adapter);
  100.         listthings.setOnItemClickListener(this);
  101.         listthings.setOnItemLongClickListener(this);
  102.         listthings.setFastScrollEnabled(true);
  103.  
  104.         addButton = (Button) findViewById(R.id.addButton);
  105.         addButton.setOnClickListener(this);
  106.         if (getIntent().getStringExtra("newPainLevel") != null) {
  107.             createNewEditedEntry();
  108.         }
  109.     }
  110.  
  111.     public class Adapter extends SimpleAdapter {
  112.         HashMap<String, String> map = new HashMap<String, String>();
  113.         View row;
  114.  
  115.         public Adapter(Context context,
  116.                 List<? extends Map<String, String>> data, int resource,
  117.                 String[] from, int[] to) {
  118.             super(context, data, resource, from, to);
  119.  
  120.         }
  121.  
  122.         @Override
  123.         public View getView(int position, View convertView, ViewGroup parent) {
  124.             row = convertView;
  125.             row = super.getView(position, convertView, parent);
  126.             if (row == null) {
  127.                 LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  128.                 row = mInflater.inflate(R.layout.mylistlayout, parent, false);
  129.             }
  130.             if (Integer.valueOf(painItems.get(position).get("row_3")) != null) {
  131.                 if (Integer.valueOf(painItems.get(position).get("row_3")) == 2) {
  132.                     row.setBackgroundColor(0xFF0000FF);
  133.                     Toast.makeText(getApplicationContext(), "this one",
  134.                             Toast.LENGTH_SHORT).show();
  135.                 }
  136.             }
  137.  
  138.             return row;
  139.  
  140.         }
  141.  
  142.     }
  143.  
  144.     private void createNewEditedEntry() {
  145.         String newPainLevel = this.getIntent().getStringExtra("newPainLevel");
  146.         String newPainTime = this.getIntent().getStringExtra("newPainTime");
  147.         String newTreatment = this.getIntent().getStringExtra("newTreatment");
  148.         painLevelString = newPainLevel;
  149.         timeOfPainString = newPainTime;
  150.         textTreatmentString = newTreatment;
  151.         painLocation = this.getIntent().getStringExtra("painLocation");
  152.         row1 = this.getIntent().getStringExtra("painLocation");
  153.         row2 = this.getIntent().getStringExtra("painDate");
  154.         // Toast.makeText(this, getIntent().getStringExtra("position"),
  155.         // Toast.LENGTH_SHORT).show();
  156.         int place = Integer.valueOf(getIntent().getStringExtra("position"));
  157.         painItems.remove(place);
  158.         adapter.notifyDataSetChanged();
  159.         addItem();
  160.  
  161.     }
  162.  
  163.     // private String getPainLevelString(){
  164.     // return painLevelString;
  165.     // }
  166.  
  167.     @Override
  168.     // on the activityresult,get the string extra, then add the item to the list
  169.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  170.         if (resultCode == 1) {
  171.             row1 = data.getStringExtra("com.painLogger.row1");
  172.             row2 = data.getStringExtra("com.painLogger.row2");
  173.             painLevelString = data.getStringExtra("com.painLogger.painLevel");
  174.             painLocation = data.getStringExtra("painLocation");
  175.             timeOfPainString = data.getStringExtra("com.painLogger.painTime");
  176.             textTreatmentString = data
  177.                     .getStringExtra("com.painLogger.treatment");
  178.             addItem();
  179.  
  180.         }
  181.     }
  182.  
  183.     // to add the item, put it in the map, and add the map into the list
  184.     private void addItem() {
  185.         HashMap<String, String> map = new HashMap<String, String>();
  186.         map.put("row_1", row1);
  187.         map.put("row_2", row2);
  188.         map.put("row_3", painLevelString);
  189.         map.put("row_4", painLocation);
  190.         map.put("row_5", timeOfPainString);
  191.         map.put("row_6", textTreatmentString);
  192.         painItems.add(map);
  193.         adapter.notifyDataSetChanged();
  194.  
  195.     }
  196.  
  197.     public void onClick(View v) {
  198.         // When '+' clicked, go to the PainLoggerActivity.java, where you can
  199.         // click enter, and that sends those strings back to here, where I can
  200.         // incorporate them into a list view, same as was there in the
  201.         // PainLogger Activity
  202.         if (v == this.addButton) {
  203.             Intent goToFields = new Intent(this, PainLoggerActivity.class);
  204.             // put the desired extras into the intent
  205.             startActivityForResult(goToFields, 1);
  206.         }
  207.  
  208.     }
  209.  
  210.     public void onItemClick(AdapterView<?> a, View v, int position, long id) {
  211.         Intent intent = new Intent(this, Item1.class);
  212.         intent.putExtra("painDate", painItems.get(position).get("row_2"));
  213.         intent.putExtra("com.painLogger.painLevel", painItems.get(position)
  214.                 .get("row_3"));
  215.         intent.putExtra("com.painLogger.painTime",
  216.                 painItems.get(position).get("row_5"));
  217.         intent.putExtra("com.painLogger.treatment", painItems.get(position)
  218.                 .get("row_6"));
  219.         intent.putExtra("painLocation", painItems.get(position).get("row_4"));
  220.         intent.putExtra("position", String.valueOf(position));
  221.         startActivity(intent);
  222.     }
  223.  
  224.     @SuppressWarnings("unchecked")
  225.     private ArrayList<HashMap<String, String>> loadListFromFile(
  226.             ArrayList<HashMap<String, String>> masterlistrev) {
  227.         try {
  228.             FileInputStream fis = openFileInput(serfilename);
  229.             ObjectInputStream ois = new ObjectInputStream(fis);
  230.             masterlistrev = (ArrayList<HashMap<String, String>>) ois
  231.                     .readObject();
  232.             ois.close();
  233.         } catch (Exception e) {
  234.             e.printStackTrace();
  235.         }
  236.         return masterlistrev;
  237.     }
  238.  
  239.     /*
  240.      * private void writeListToFile( ArrayList<HashMap<String, String>>
  241.      * masterlistrev, Context ctx, String filename) {
  242.      *
  243.      * FileOutputStream fos; try { fos = ctx.openFileOutput(filename,
  244.      * Context.MODE_PRIVATE); ObjectOutputStream oos = new
  245.      * ObjectOutputStream(fos); oos.writeObject(masterlistrev); oos.close(); }
  246.      * catch (FileNotFoundException e) { e.printStackTrace(); } catch
  247.      * (IOException e) { e.printStackTrace(); }
  248.      *
  249.      * }
  250.      */
  251.     private void writeListToFile(
  252.             ArrayList<HashMap<String, String>> masterlistrev, Context ctx,
  253.             String filename) {
  254.         FileOutputStream fos;
  255.         ObjectOutputStream oos = null;
  256.         try {
  257.             fos = ctx.openFileOutput(filename, Context.MODE_PRIVATE);
  258.             oos = new ObjectOutputStream(fos);
  259.             oos.writeObject(masterlistrev);
  260.         } catch (FileNotFoundException e) {
  261.             e.printStackTrace();
  262.         } catch (IOException e) {
  263.             e.printStackTrace();
  264.         } finally {
  265.             try {
  266.                 oos.close();
  267.             } catch (IOException e) {
  268.  
  269.                 e.printStackTrace();
  270.             }
  271.         }
  272.     }
  273.  
  274.     protected void onStop() {
  275.         super.onStop();
  276.         writeListToFile((ArrayList<HashMap<String, String>>) painItems,
  277.                 getApplicationContext(), serfilename);
  278.     }
  279.  
  280.     protected void onDestroy() {
  281.         super.onDestroy();
  282.         writeListToFile((ArrayList<HashMap<String, String>>) painItems,
  283.                 getApplicationContext(), serfilename);
  284.     }
  285.  
  286.     @Override
  287.     public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
  288.             final int position, long id) {
  289.         AlertDialog.Builder builder = new AlertDialog.Builder(this);
  290.         builder.setMessage("Do you want to delete this Item?")
  291.                 .setCancelable(false)
  292.                 .setPositiveButton("Yes",
  293.                         new DialogInterface.OnClickListener() {
  294.                             public void onClick(DialogInterface dialog, int id) {
  295.  
  296.                                 painItems.remove(position);
  297.                                 adapter.notifyDataSetChanged();
  298.  
  299.                             }
  300.                         })
  301.                 .setNegativeButton("No", new DialogInterface.OnClickListener() {
  302.                     public void onClick(DialogInterface dialog, int id) {
  303.                         dialog.cancel();
  304.                     }
  305.                 });
  306.         AlertDialog alert = builder.create();
  307.  
  308.         alert.show();
  309.  
  310.         return false;
  311.     }
  312.  
  313. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement