Esercizio 3 per casa

Creare un programma per Android che legga e scriva nella tabella “dati” contenuta nel database “Studente.db” i dati: nome, indirizzo ed email.

-------------------AndroidManifest.xml-------------------

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="org.me.dbstudente">
    <application>
         <activity android:name=".Agenda" android:label="Agenda">
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
        <activity android:name=".Lista"></activity>
    </application>
</manifest>

 

-------------------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:padding="10px"
            android:text="Mostra detagli" />
      </RelativeLayout>

    </LinearLayout>
  </ScrollView>

</RelativeLayout>

-------------------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="Nome:"/>
        <EditText android:id="@+id/nome"
                  android:hint="Scrivi il tuo nome"
        />
    </TableRow>
        <TableRow>
        <TextView android:text="Indirizzo:"/>
        <EditText android:id="@+id/via"
                  android:hint="Scrivi il tuo indirizzo"
        />
    </TableRow>
        <TableRow>
        <TextView android:text="E-mail:"/>
        <EditText android:id="@+id/email"
                  android:hint="Scrivi la tua e-mail"
        />
    </TableRow>
        <Button android:id="@+id/salva"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="Salva"
          />
        <Button android:id="@+id/impegni"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:text="visualizza i dati"
          />
</TableLayout>

-------------------Agenda.java-------------------

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package org.me.dbstudente;

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 Lorenzo Millucci
*/
public class Agenda 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);
        creaDB();
        creaTabella();
        Button salva=(Button)findViewById(R.id.salva);
        Button lista=(Button)findViewById(R.id.impegni);
        salva.setOnClickListener(salva1);
        lista.setOnClickListener(onLista);
    }

    private View.OnClickListener onLista=new View.OnClickListener(){
         public void onClick(View v){
             Intent i=new Intent(v.getContext(),Lista.class);
             startActivity(i);
             }
         };

    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 dati (_id integer primary key autoincrement, nome varchar(20), via varchar(50), email varchar(50));");
            }
        }catch(SQLiteException e){
            e.printStackTrace();
        }
    }

    private View.OnClickListener salva1=new View.OnClickListener(){
         public void onClick(View v){
             EditText appunto1=(EditText)findViewById(R.id.nome);
             EditText dove1=(EditText)findViewById(R.id.via);
             EditText quando1=(EditText)findViewById(R.id.email);
             String nome=appunto1.getText().toString();
             String via=dove1.getText().toString();
             String email=quando1.getText().toString();
             //dove1.setText("");
             String inserisci="Insert into dati (nome,via,email) values ('"+nome+"','"+via+"','"+email+"');";
//             String inserisci="Insert into dati (nome,via,email) values ('"+nome+"',"+via+",'"+email+"');";
//                            String inserisci="Insert into voto (datavoto,voto,motivo,materia,prof,nome) values ('"+
//              datavoto+"',"+voto+",'"+motivo+"','"+materia+"','"+prof+"','"+nome+"');";

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

     @Override
     public void onDestroy() {
          super.onDestroy();
          database.close();
     }
}

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

package org.me.dbstudente;
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 = "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.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("dati",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.nome = c.getString(1);
                              r.via = c.getString(2);
                              r.email = c.getString(3);
                              String rr=r.nome+" "+r.via+" "+r.email;
                              System.out.println(rr);
                              righe.add(rr);
                              c.moveToNext();
                    }
     }
     class Riga {
          public long _id;
          public String nome;
          public String via;
          public String email;
     }
     private View.OnClickListener onInserisci=new View.OnClickListener(){
         public void onClick(View v) {
            Intent i = new Intent(v.getContext(), Agenda.class);
            startActivity(i);
         }
      };  
     @Override
     public void onDestroy() {
          super.onDestroy();
          database.close();
     }
}

 

Senza titolo Senza titolo2

Commenti

Posta un commento