|
inserisco anche il codice
MANIFEST <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="matteo.android.SpedyGo" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="9" />
<application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".Form" 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=".Result" android:label="@string/result"> </activity> </application> </manifest>
Form Activity
package matteo.android.SpedyGo;
import android.app.Activity; import android.content.ComponentName; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.AutoCompleteTextView;
public class Form extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); }
public void onAddClick(View botton) { AutoCompleteTextView spedID = (AutoCompleteTextView) findViewById(R.id.SpedID); AutoCompleteTextView spedCliente = (AutoCompleteTextView) findViewById(R.id.SpedCliente); AutoCompleteTextView spedCorriere = (AutoCompleteTextView) findViewById(R.id.SpedCorriere);
Intent intent = new Intent(); intent.setClass(this, Result.class);
intent.putExtra("SpedID", spedID.getText().toString()); intent.putExtra("SpedCliente", spedCliente.getText().toString()); intent.putExtra("SpedCorriere", spedCorriere.getText().toString()); startActivity(intent); }
public void onCancelClick(View botton) { Intent intent = new Intent(); intent.setComponent(new ComponentName(this, Result.class)); intent.putExtra("Cancel", "Cancel"); startActivity(intent); }
}
RESULT ACTIVITY
package matteo.android.SpedyGo;
import android.app.Activity; import android.content.ContentValues; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.widget.TextView;
public class Result extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.result);
TextView resultText = (TextView) findViewById(R.id.resultText); Bundle bundle = getIntent().getExtras();
if (bundle.getString("Cancel") != null) { resultText.setText(getString(R.string.cancelOp)); } else { String spedID = bundle.getString("SpedID"); String spedCliente = bundle.getString("SpedCliente"); String spedCorriere = bundle.getString("SpedCorriere"); insertSpedizione(spedID, spedCliente, spedCorriere); // metodo per resultText.setText(getString(R.string.resultOk) + "\n" + spedID + "\n" + spedCliente + "\n" + spedCorriere); } }
private void insertSpedizione(String idsped, String cliente, String idcorriere) { DatabaseHelper databaseHelper = new DatabaseHelper(this); SQLiteDatabase db = databaseHelper.getWritableDatabase(); // creo un
ContentValues cv = new ContentValues(); // Hashmap cv.put(DatabaseHelper.IDSPED, idsped); // nome colonna, valore cv.put(DatabaseHelper.CLIENTE, cliente);// nome colonna, valore cv.put(DatabaseHelper.IDCORRIERE, idcorriere);// nome colonna, valore
db.insert("spedizioni", DatabaseHelper.IDSPED, cv); // faccio un insert db.close(); // quando abbiamo finito chiudiamo }
}
CREO IL DATABASE
package matteo.android.SpedyGo;
import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper { public static final String DATABASE_NAME = "datasped.db"; // no public static final String IDSPED = "idsped"; public static final String CLIENTE = "cliente"; public static final String IDCORRIERE = "idcorriere";
public DatabaseHelper(Context context) { super(context, DATABASE_NAME, null, 1); // 1 è la versione iniziale del }
@Override public void onCreate(SQLiteDatabase db) { // onCreate crea la db.execSQL("CREATE TABLE spedizioni (id_INTEGER PRIMARY KEY AUTOINCREMENT, idsped TEXT, cliente TEXT, idcorriere TEXT"); }
@Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // cambia android.util.Log.w("spedizioni", "Upgrading Database, verranno cancellati tutti i vecchi dati"); db.execSQL("DROP TABLE IF EXIST spedizioni"); onCreate(db); // ricreo il Database } } classe R /* AUTO-GENERATED FILE. DO NOT MODIFY. * * This class was automatically generated by the * aapt tool from the resource data it found. It * should not be modified by hand. */
package matteo.android.SpedyGo;
public final class R { public static final class attr { } public static final class drawable { public static final int icon=0x7f020000; } public static final class id { public static final int SpedCliente=0x7f050002; public static final int SpedCorriere=0x7f050003; public static final int SpedID=0x7f050001; public static final int linearLayout1=0x7f050000; public static final int linearLayout2=0x7f050004; public static final int resultText=0x7f050005; } public static final class layout { public static final int main=0x7f030000; public static final int result=0x7f030001; } public static final class string { public static final int InserisciSpedizione=0x7f040002; public static final int app_name=0x7f040001; public static final int cancel=0x7f040006; public static final int cancelOp=0x7f04000a; public static final int confirm=0x7f040007; public static final int hello=0x7f040000; public static final int result=0x7f040008; public static final int resultOk=0x7f040009; public static final int textCorriere=0x7f040005; public static final int textcliente=0x7f040004; public static final int textsped=0x7f040003; } } MAIN 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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/InserisciSpedizione" android:textSize="30sp" android:textStyle="bold" android:gravity="center" /> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/textsped" android:textStyle="bold" android:padding="5sp" android:width="120sp" android:textSize="16sp"></TextView> <AutoCompleteTextView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:width="500sp" android:id="@+id/SpedID"></AutoCompleteTextView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:padding="5sp" android:text="@string/textcliente" android:textSize="16sp" android:width="120sp"></TextView> <AutoCompleteTextView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:width="500sp" android:id="@+id/SpedCliente"></AutoCompleteTextView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout1"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textStyle="bold" android:padding="5sp" android:text="@string/textCorriere" android:textSize="16sp" android:width="120sp"></TextView> <AutoCompleteTextView android:layout_weight="1" android:layout_width="match_parent" android:layout_height="wrap_content" android:width="500sp" android:id="@+id/SpedCorriere"></AutoCompleteTextView> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2"> <Button android:layout_height="wrap_content" android:text="@string/cancel" android:layout_width="130sp" android:onClick="onCancelClick"></Button> <Button android:layout_height="wrap_content" android:text="@string/confirm" android:layout_width="130sp" android:onClick="onAddClick"></Button> </LinearLayout> </LinearLayout> STRING XML <?xml version="1.0" encoding="utf-8"?> <resources> <string name="hello">Hello World, Form!</string> <string name="app_name">SpedyGo</string> <string name="InserisciSpedizione">Inserisci Spedizione</string> <string name="textsped">Id Spedizione</string> <string name="textcliente">Cliente</string> <string name="textCorriere">Id Corriere</string> <string name="cancel">Cancella</string> <string name="confirm">Conferma</string>
<string name="result">Risultato</string> <string name="resultOk">Operazione Avvenuta con Successo</string> <string name="cancelOp">Operazione Cancellata</string> </resources> RESULT 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"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" android:focusableInTouchMode="false" android:textSize="25sp" android:padding="10sp" android:gravity="center" android:text="@string/result" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:textStyle="bold" android:focusableInTouchMode="false" android:textSize="20sp" android:padding="10sp" android:gravity="center" android:text="" android:id="@+id/resultText" /> </LinearLayout>
|