1. package ur.grades;
  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.io.Serializable;
  10. import java.io.StreamCorruptedException;
  11. import java.util.ArrayList;
  12.  
  13. import android.app.Activity;
  14. import android.app.AlertDialog;
  15. import android.content.Context;
  16. import android.content.DialogInterface;
  17. import android.content.Intent;
  18. import android.os.Bundle;
  19. import android.text.Editable;
  20. import android.view.ContextMenu;
  21. import android.view.LayoutInflater;
  22. import android.view.Menu;
  23. import android.view.MenuInflater;
  24. import android.view.MenuItem;
  25. import android.view.View;
  26. import android.view.ContextMenu.ContextMenuInfo;
  27. import android.widget.AdapterView;
  28. import android.widget.ArrayAdapter;
  29. import android.widget.EditText;
  30. import android.widget.LinearLayout;
  31. import android.widget.ListAdapter;
  32. import android.widget.ListView;
  33. import android.widget.AdapterView.OnItemClickListener;
  34. import android.widget.Toast;
  35.  
  36. public class Start extends Activity implements Serializable {
  37.     private ListView listView;
  38.     private ArrayList<String> lista = new ArrayList<String>();
  39.     private ArrayList<String> cadeiras = new ArrayList<String>();
  40.     private ArrayList<String> notas = new ArrayList<String>();
  41.     private ArrayAdapter<String> adapter = null;
  42.     private static String fich = "gravar.dat";
  43.     EditText aux;
  44.     LinearLayout dialerLayout;
  45.    
  46.  
  47.     @Override
  48.     public void onCreate(Bundle savedInstanceState) {
  49.         super.onCreate(savedInstanceState);
  50.         setContentView(R.layout.listagem);
  51.          listView = (ListView)findViewById(R.id.listView1);
  52.         listView.setTextFilterEnabled(true);
  53.         listView.setChoiceMode( 1);
  54.         registerForContextMenu(listView);
  55.        
  56.  
  57.        
  58.         listView.setOnItemClickListener(new OnItemClickListener() {
  59.  
  60.             public void onItemClick(AdapterView<?> parent, View view,
  61.                     int position, long id) {
  62.                
  63.             }
  64.  
  65.        
  66.         });
  67.     }
  68.  
  69.     @Override
  70.     protected void onStart() {
  71.        
  72.         super.onStart();
  73.          FileInputStream fin;
  74.         try {
  75.             fin = new FileInputStream(fich);
  76.             ObjectInputStream in = new ObjectInputStream(fin);
  77.             carregar(in);
  78.  
  79.         } catch (FileNotFoundException e) {
  80.             // TODO Auto-generated catch block
  81.             e.printStackTrace();
  82.         } catch (StreamCorruptedException e) {
  83.             // TODO Auto-generated catch block
  84.             e.printStackTrace();
  85.         } catch (IOException e) {
  86.             // TODO Auto-generated catch block
  87.             e.printStackTrace();
  88.         } catch (ClassNotFoundException e) {
  89.             // TODO Auto-generated catch block
  90.             e.printStackTrace();
  91.         }
  92.         adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, lista);
  93.         listView.setAdapter(adapter);
  94.        
  95.     }
  96.    
  97.    
  98.     @Override
  99.     protected void onPause() {
  100.         // TODO Auto-generated method stub
  101.         super.onPause();
  102.        
  103.          ObjectOutputStream out;
  104.            try {
  105.             out = new ObjectOutputStream(new FileOutputStream(fich));
  106.             gravar(out);
  107.         } catch (FileNotFoundException e) {
  108.             // TODO Auto-generated catch block
  109.             e.printStackTrace();
  110.         } catch (IOException e) {
  111.             // TODO Auto-generated catch block
  112.             e.printStackTrace();
  113.         }
  114.     }
  115.  
  116.     //Menu de contexto
  117.     public void onCreateContextMenu(ContextMenu menu, View v,  
  118.             ContextMenuInfo menuInfo) {  
  119.             super.onCreateContextMenu(menu, v, menuInfo);  
  120.            
  121.             menu.setHeaderTitle("Opções");  
  122.             MenuInflater inflater = getMenuInflater();  
  123.             inflater.inflate(R.menu.menu, menu);  
  124.             }  
  125.    
  126.     //Ao criar a aplicação, cria o menu com as devidas opções.
  127.     public boolean onCreateOptionsMenu(Menu menu) {  
  128.         MenuInflater inflater = getMenuInflater();  
  129.         inflater.inflate(R.menu.menu_principal, menu);  
  130.         return true;  
  131.         }
  132.    
  133.    
  134.   // O que fazer quando se clica num botão do Menu
  135.     public boolean onOptionsItemSelected(MenuItem item) {  
  136.         switch (item.getItemId()) {  
  137.         case R.id.adicionar:  
  138.             //Toast.makeText(this, "This is the Toast message", Toast.LENGTH_LONG).show();
  139.             final AlertDialog.Builder alert = new AlertDialog.Builder(this);
  140.             final EditText input = new EditText(this);
  141.            
  142.              LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  143.              dialerLayout = (LinearLayout) layoutInflater.inflate(R.layout.input, null);
  144.              LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
  145.                 dialerLayout.setLayoutParams(params);
  146.            
  147.            
  148.             alert.setView(dialerLayout);
  149.            
  150.            
  151.            
  152.             alert.setPositiveButton("Adicionar", new DialogInterface.OnClickListener() {
  153.                 public void onClick(DialogInterface dialog, int whichButton) {
  154.                    
  155.  
  156.                     aux = (EditText)dialerLayout.findViewById(R.id.cadeira);
  157.                
  158.                     String value = aux.getText().toString();
  159.                     aux = (EditText)dialerLayout.findViewById(R.id.nota);
  160.                     int nota = Integer.parseInt(aux.getText().toString());
  161.                     notas.add(String.valueOf(nota));
  162.                     cadeiras.add(value);
  163.                     String finalz = value+" : "+nota;
  164.                     lista.add(finalz);
  165.                  adapter.notifyDataSetChanged();
  166.                
  167.                 }
  168.            
  169.             });
  170.  
  171.             alert.setNegativeButton("Cancelar",
  172.                     new DialogInterface.OnClickListener() {
  173.                         public void onClick(DialogInterface dialog, int whichButton) {
  174.                             dialog.cancel();
  175.                         }
  176.                     });
  177.             alert.show();
  178.  
  179.            
  180.            
  181.         return true;  
  182.        
  183.         case R.id.media:
  184.             calcular_media();
  185.             return true;
  186.            
  187.         case R.id.gravar:
  188.             ObjectOutputStream out;
  189.            
  190.                try {
  191.                     out = new ObjectOutputStream(new FileOutputStream(fich));
  192.                     gravar(out);
  193.                        Toast.makeText(getApplicationContext(), "nice!", Toast.LENGTH_LONG).show();
  194.  
  195.                 } catch (FileNotFoundException e) {
  196.                     // TODO Auto-generated catch block
  197.                    Toast.makeText(getApplicationContext(), "error1!", Toast.LENGTH_LONG).show();
  198.  
  199.                     e.printStackTrace();
  200.                 } catch (IOException e) {
  201.                    Toast.makeText(getApplicationContext(), "error2!", Toast.LENGTH_LONG).show();
  202.  
  203.                     e.printStackTrace();
  204.                 }
  205.             return true;
  206.         default: return super.onOptionsItemSelected(item);  
  207.         }
  208.         }
  209.  
  210.     @Override
  211.     public boolean onContextItemSelected(MenuItem item) {
  212.        
  213.          AdapterView.AdapterContextMenuInfo contextMenuInfo = (AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
  214.        
  215.          if(item.getItemId()==R.id.item2){
  216.                 lista.remove(contextMenuInfo.position);
  217.                 notas.remove(contextMenuInfo.position);
  218.                 cadeiras.remove(contextMenuInfo.position);
  219.                 adapter.notifyDataSetChanged();
  220.                
  221.                 return true;
  222.                 }
  223.          
  224.          return false;
  225.     }  
  226.    
  227.      public void calcular_media(){  
  228.  
  229.          double contador=0;
  230.          double soma_notas=0;
  231.          double media=0;
  232.          for(int i=0;i<notas.size();i++){
  233.              contador++;
  234.              soma_notas+=Double.parseDouble(notas.get(i));
  235.          }
  236.          
  237.            media= (soma_notas)/(contador);
  238.            
  239.            Toast.makeText(getApplicationContext(), "Média = "+media, Toast.LENGTH_LONG).show();
  240.         }
  241.  
  242.       public void gravar(ObjectOutputStream out) throws IOException {
  243.           out.writeObject(lista);
  244.           out.writeObject(cadeiras);
  245.           out.writeObject(notas);
  246.           out.close();
  247.       }
  248.      
  249.     public void carregar(ObjectInputStream in) throws IOException, ClassNotFoundException {
  250.          lista=(ArrayList<String>) in.readObject();
  251.          cadeiras=(ArrayList<String>) in.readObject();
  252.          notas= (ArrayList<String>) in.readObject();
  253.           in.close();
  254.       }
  255.      
  256.      
  257.    
  258. }