Applicazione per salvare i voti in un database

Android Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.database.tutorial"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:label="@string/app_name">
        <activity android:name="Studente"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ListaVoti"></activity>
    </application>
</manifest>

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

ListaVoti

package com.database.tutorial;
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;
public class ListaVoti extends Activity {
     private final String DB_NAME = "Studente.db";   
     private SQLiteDatabase database = null;
     private ListView voti;
     ArrayList<String> righe = new ArrayList<String>();
     private String lvoti[]={"5 informatica 10/02/2010","7 italiano 31/01/2010","9 educazione fisica 25/03/2010","7 inglese 28/02/2010"};
     /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.listavoti);
         creaDatabase();
         leggiTabellaVoto();
         voti=(ListView)findViewById(R.id.listavoti);
           // By using setAdpater method in listview we an add string array in list.
         voti.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1 , righe));
    }
    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("voto",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.datav = c.getString(1);
                              r.v = c.getString(2);
                              r.motivo = c.getString(3);
                              r.materia=c.getString(4);
                              r.prof=c.getString(5);
                              String rr=r.v+" "+r.datav+" "+r.materia+" "+r.prof;
                              righe.add(rr);
                              c.moveToNext();
                    }
     }
     class Riga {
          public long _id;
          public String v;
          public String datav;
          public String motivo;
          public String materia;
          public String prof;
          public String nome;
     }
     @Override
     public void onDestroy() {
          super.onDestroy();
          database.close();
     }
}

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

Studente

package com.database.tutorial;
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;
public class Studente extends Activity {
     private final String DB_NAME = "Studente.db";   
     private SQLiteDatabase database = null;
      /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         creaDatabase();
         creaTabellaVoto();
         Button save=(Button)findViewById(R.id.save);
         Button lista = (Button) findViewById(R.id.listavoti);
         save.setOnClickListener(onSave);
         lista.setOnClickListener(onLista);
    }
    private View.OnClickListener onSave=new View.OnClickListener(){
         public void onClick(View v){
               EditText datavoto1=(EditText)findViewById(R.id.datavoto);
               EditText  voto1=(EditText)findViewById(R.id.voto);
               EditText motivo1=(EditText)findViewById(R.id.motivo);
               EditText materia1=(EditText)findViewById(R.id.materia);
               EditText prof1=(EditText)findViewById(R.id.prof);
               EditText nome1=(EditText)findViewById(R.id.nome);
               int voto=Integer.parseInt(voto1.getText().toString());
               String datavoto=datavoto1.getText().toString();
               String motivo=motivo1.getText().toString();
               String materia=materia1.getText().toString();
               String prof=prof1.getText().toString();
               String nome=nome1.getText().toString();
               String inserisci="Insert into voto (datavoto,voto,motivo,materia,prof,nome) values ('"+
              datavoto+"',"+voto+",'"+motivo+"','"+materia+"','"+prof+"','"+nome+"');";
               try {
                         database.execSQL(inserisci);
                         System.out.println("inserimento avvenuto");
               } catch (SQLiteException e){
                    System.out.println("inserimento non avvenuto");
                    e.printStackTrace();
               }
         }
    };
      private View.OnClickListener onLista=new View.OnClickListener(){
         public void onClick(View v) {
            Intent i = new Intent(v.getContext(), ListaVoti.class);
            startActivity(i);
         }
      };          
    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 creaTabellaVoto(){
          try{    
                    System.out.println("sono qui dentro la tabella: "+database.toString());
                    if (database.isOpen()){
                         database.execSQL("CREATE TABLE IF NOT EXISTS voto(_id integer primary key autoincrement, datavoto char(10), voto integer, motivo text, materia varchar(50), prof varchar(50), nome varchar(50));");
                    }
          } catch (SQLiteException e) {
                    System.out.println("non ha creato la tabella voto");
                    e.printStackTrace();
          }
     }
     @Override
     public void onDestroy() {
          super.onDestroy();
          database.close();
     }
}

 

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

listavoti.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:orientation="vertical"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
>
     <ListView android:id="@+id/listavoti"
    android:layout_width="wrap_content"    
    android:layout_height="wrap_content" />

</LinearLayout>

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

<?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="I miei voti:" />
                    <EditText android:id="@+id/nome" />
          </TableRow>
          <TableRow>
                    <TextView android:text="data:" />
                    <EditText android:id="@+id/datavoto" />
          </TableRow>
          <TableRow>
                    <TextView android:text="voto:" />
                    <EditText android:id="@+id/voto" />
          </TableRow>
          <TableRow>
                    <TextView android:text="motivo:" />
                    <EditText android:id="@+id/motivo" />
          </TableRow>
          <TableRow>
                    <TextView android:text="materia:" />
                    <EditText android:id="@+id/materia" />
          </TableRow>
          <TableRow>
                    <TextView android:text="prof:" />
                    <EditText android:id="@+id/prof" />
          </TableRow>
          <Button android:id="@+id/save"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="Inserisci"
          />
          <Button android:id="@+id/listavoti"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="visualizza i miei voti"
          />
</TableLayout>

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

1 2

Commenti

  1. Bravo Millucci, anche da parte di tutti i Prof. e Prof.sse del corso Rete4

    RispondiElimina

Posta un commento