Calcolatrice con Database

Calcolatrice salva le somme eseguite in un database

-------------------CalcolatriceDB.java-------------------

package org.me.calcolatricedb;

import android.app.Activity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.content.Context;
import android.widget.*;
import android.view.View;
import android.content.Intent;

/**
*
* @author lollino360
*/
public class CalcolatriceDB extends Activity {
        private final String DB_NAME="Calcolatrice.db";
        private SQLiteDatabase database = null;
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);
        Button somma=(Button)findViewById(R.id.somma);
        somma.setOnClickListener(somma1);
        creaDB();
        creaTabella();
        Button mostra=(Button)findViewById(R.id.mostra);
        mostra.setOnClickListener(onMostra);
        // ToDo add your GUI initialization code here
    }
    private View.OnClickListener somma1=new View.OnClickListener() {
        public void onClick(View v){
            EditText numero1=(EditText)findViewById(R.id.num1);
            EditText numero2=(EditText)findViewById(R.id.num2);
            EditText ris1=(EditText)findViewById(R.id.ris);
            int num1=Integer.parseInt(numero1.getText().toString());
            int num2=Integer.parseInt(numero2.getText().toString());
            int ris=num1+num2;
            String risultato=num1+"+"+num2+"="+ris;
            ris1.setText(risultato);
//            numero1.setText(" ");
//            numero2.setText(" ");
            String inserisci="Insert into conti (operazione) values ('"+risultato+"');";

            try{
                 database.execSQL(inserisci);
             }catch(SQLiteException e){
                 e.printStackTrace();
             }

        }
    };
    public void creaDB(){
        try{
            database=openOrCreateDatabase(DB_NAME,SQLiteDatabase.CREATE_IF_NECESSARY,null);
        }catch(SQLiteException e){
            e.printStackTrace();
        }
    }
    public void creaTabella(){
        try{
            if (database.isOpen()){
                         database.execSQL("CREATE TABLE IF NOT EXISTS conti (_id integer primary key autoincrement, operazione varchar(50));");
            }
        }catch(SQLiteException e){
            e.printStackTrace();
        }
    }
     private View.OnClickListener onMostra=new View.OnClickListener(){
         public void onClick(View v) {
            Intent i = new Intent(v.getContext(), Lista.class);
            startActivity(i);
         }
      };

}

-------------------Lista.java-------------------

package org.me.calcolatricedb;

import android.app.Activity;
import android.os.Bundle;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.content.Context;
import android.widget.*;
import android.view.View;
import android.widget.ListView;
import android.widget.AdapterView;
import android.database.Cursor;
import java.util.ArrayList;
import android.util.Log;
import android.content.Intent;
public class Lista extends Activity {
     private final String DB_NAME = "Calcolatrice.db";
     private SQLiteDatabase database = null;
     private ListView voti;
     ArrayList<String> righe = new ArrayList<String>();
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.lista);
         creaDatabase();
         leggiTabellaVoto();
         voti=(ListView)findViewById(R.id.listav);
           // By using setAdpater method in listview we an add string array in list.
         //Log.i("voti="+voti);
         voti.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , righe));
         Button inserisci = (Button) findViewById(R.id.btnGetMoreResults);
         inserisci.setOnClickListener(onInserisci);
    }
    public void creaDatabase(){
         try{
                    database=openOrCreateDatabase(DB_NAME,SQLiteDatabase.CREATE_IF_NECESSARY, null);
          } catch (SQLiteException e) {
                    System.out.println("non ha creato il database studente");
                    e.printStackTrace();
          }
     }
     public void leggiTabellaVoto(){

                    Cursor c=database.query("conti",null,null,null,null,null,null,null);
                     int numRows = c.getCount();
                    c.moveToFirst();
                    for (int i = 0; i < numRows; ++i) {
                              Riga r = new Riga();
                              r._id = c.getLong(0);
                              r.operazione = c.getString(1);
                              System.out.println(r.operazione);
                              righe.add(r.operazione);
                              c.moveToNext();
                    }

     }

     class Riga {
          public long _id;
          public String operazione;
     }
     private View.OnClickListener onInserisci=new View.OnClickListener(){
         public void onClick(View v) {
            Intent i = new Intent(v.getContext(), CalcolatriceDB.class);
            startActivity(i);
         }
      };
     @Override
     public void onDestroy() {
          super.onDestroy();
          database.close();
     }
}

-------------------main.xml-------------------

<?xml version="1.0" encoding="UTF-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
    <TableRow>
        <TextView android:text="numero:"
                  android:layout_width="100px"
                  />
    <EditText
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:id="@+id/num1"
    />
    </TableRow>
        <TableRow>
        <TextView android:text="numero:"
                  android:layout_width="100px"
                  />
    <EditText
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:id="@+id/num2"
    />
    </TableRow>
    <TableRow>
        <TextView android:text="Risultato:"
                  android:layout_width="100px"
                  />
    <EditText
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:id="@+id/ris"
    />
    </TableRow>
        <Button android:id="@+id/somma"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="+"
          />
        <Button android:id="@+id/mostra"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Mostra operazioni eseguite"
          />
</TableLayout>

-------------------lista.xml-------------------

<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     >

  <ScrollView
          android:layout_height="fill_parent"
          android:layout_width="fill_parent">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
            >
      <ListView android:id="@+id/listav"
          android:layout_width="fill_parent"
          android:layout_height="300sp"
          android:drawSelectorOnTop="false"
          android:cacheColorHint="#ff6a00"
          android:divider="#ff8f40"
          android:dividerHeight="2px"
          />
      <RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#676767"
          android:orientation="vertical">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
           android:id="@+id/btnGetMoreResults"
            android:padding="10px"
            android:text="Inserisci nuovo dato" />
      </RelativeLayout>
      <RelativeLayout
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"
          android:background="#676767"
          android:orientation="vertical">

        <Button
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
           android:id="@+id/btnGetMoreResults1"
             android:visibility="gone"
            android:padding="10px"
            android:text="Mostra detagli" />
      </RelativeLayout>

    </LinearLayout>
  </ScrollView>

</RelativeLayout>

---------------------------------------------------------

 2 3

Commenti

  1. Hai fatto un gran lavoro. Bravo. Spero che funzioni. Nel frattempo ho trovato un modo che semplifica la scrittura del metodo associato al bottone. Fra poco pubblico l'esercizio svolto in maniera molto semplice per tutta la classe.

    RispondiElimina

Posta un commento