Agendapersonal

11

Click here to load reader

Transcript of Agendapersonal

Page 1: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

Materia

Desarrollo de aplicaciones III

Tema

Documentación aplicación agendapersonal

Nombre completo del Alumno : Marycruz Santos Escañero

Grado: 5 Grupo: “B”

Nombre del Docente : Eloy Contreras De Lira

Fecha de entrega : 24/03/2014

Page 2: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

Agenda personal en android

En primer lugar crearemos el entorno visual de la aplicación, añadiremos cuatro cuadros de texto para introducir nombre, teléfono, dirección y email del contacto y

añadiremos cuatro botones: guardar, modificar, consultar y eliminar contacto. Para ello accederemos en el Package Explorer a "res" luego a "layout" y haremos doble click en "main.xml":

Page 3: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

El fichero "main.xml" contendrá en formato XML los componentes visuales de nuestra

aplicación Android, desde aquí especificaremos los elementos que verá el usuario de

nuestra aplicación Android

Ahora creamos una nueva clase para crear la base de datos que contendrá los

registros de los contactos aquí se está creando la tabla contactos con los campos

nombre tipo texto, teléfono tipo integer, dirección y email tipo texto

Ahora procedemos con la clase MainActivity.java en esta codificamos la

funcionalidad de los botones de la parte grafica.

Se agregan las librerías necesarias

Page 4: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

Declaramos variables para donde identificamos la id de los componentes gráficos

para posteriormente utilizarlos

Esta parte corresponde a la función que tendrá el botón guardar, se hace

referencia a la tabla que creamos anteriormente donde los valores que se

encuentren en el área de texto por ejemplo et1 se almacenara en el campo

nombre tipo string y así con los demás campos

Page 5: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

Se realiza la consulta a través del campo nombre si en el área de texto et1 se

encuentra un valor que se encuentre en la tabla contacto entonces muestra la

información del contacto de lo contrario manda un mensaje donde nos dice que la

tabla no existe

Page 6: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

Hace la consulta del registro si el contacto existe lo elimina al pulsar el botón

eliminar de lo contrario manda un mensaje donde el registro no existe

En esta parte de código se realiza la modificación el proceso es el mismo se

realiza la consulta y si el registro existe es posible realizar las modificaciones a los

datos del proyecto

Page 7: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

Aquí se muestra el código completo de la aplicación

MainActivity.java import android.app.Activity; import android.content.ContentValues; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.os.Bundle; import android.view.Menu; import android.view.View; import android.widget.EditText; import android.widget.Toast;

public class MainActivity extends Activity { private EditText et1, et2, et3, et4;

@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main); et1 = (EditText) findViewById(R.id.etnombre); et2 = (EditText) findViewById(R.id.ettelefono); et3 = (EditText) findViewById(R.id.etdireccion); et4 = (EditText) findViewById(R.id.etemail); } @Override public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.activity_main, menu); return true;

} public void guardar(View v) { AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1); SQLiteDatabase bd = admin.getWritableDatabase(); String nombre = et1.getText().toString(); String telefono = et2.getText().toString(); String direccion = et3.getText().toString(); String email = et4.getText().toString(); ContentValues registro = new ContentValues();

registro.put("nombre", nombre); registro.put("telefono", telefono); registro.put("direccion", direccion); registro.put("email", email); bd.insert("contactos", null, registro);

bd.close();

Page 8: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

et1.setText(""); et2.setText(""); et3.setText(""); et4.setText(""); Toast.makeText(this, "se a registrado el contacto",

Toast.LENGTH_SHORT).show(); } public void consultar(View v) { AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);

SQLiteDatabase bd = admin.getWritableDatabase(); String nombre = et1.getText().toString(); Cursor fila = bd.rawQuery( "select nombre,telefonio,direccion,email from contactos where nombre=" + nombre + "", null); if (fila.moveToFirst()) {

et2.setText(fila.getString(0)); et3.setText(fila.getString(1)); et4.setText(fila.getString(2)); } else Toast.makeText(this, "No existe el contacto",

Toast.LENGTH_SHORT).show(); bd.close(); } public void eliminar(View v) { AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);

SQLiteDatabase bd = admin.getWritableDatabase(); String nombre = et1.getText().toString(); int cant = bd.delete("contactos", "nombre=" +nombre + "", null); bd.close(); et1.setText(""); et2.setText(""); et3.setText(""); et4.setText(""); if (cant == 1) Toast.makeText(this, "Se a eliminado el contacto",

Toast.LENGTH_SHORT).show(); else Toast.makeText(this, "No existe el contacto",

Toast.LENGTH_SHORT).show(); } public void modificar(View v) { AdminSQLiteOpenHelper admin = new AdminSQLiteOpenHelper(this, "administracion", null, 1);

SQLiteDatabase bd = admin.getWritableDatabase(); String nombre = et1.getText().toString(); String telefono = et2.getText().toString();

Page 9: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

String direccion = et3.getText().toString(); String email= et4.getText().toString(); ContentValues registro = new ContentValues();

registro.put("nombre", nombre); registro.put("telefono", telefono); registro.put("direccion", direccion); registro.put("email", email); int cant = bd.update("contactos", registro, "nombre=" + nombre, null);

bd.close(); if (cant == 1) Toast.makeText(this, "se modificaron los datos", Toast.LENGTH_SHORT)

.show(); else Toast.makeText(this, "no existe el contacto",

Toast.LENGTH_SHORT).show(); } }

AdminSQLiteOpenHelper.java import android.content.Context; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase.CursorFactory; import android.database.sqlite.SQLiteOpenHelper; public class AdminSQLiteOpenHelper extends SQLiteOpenHelper {

public AdminSQLiteOpenHelper(Context context, String nombre, CursorFactory factory, int version) { super(context, nombre, factory, version);

} @Override public void onCreate(SQLiteDatabase db) {

db.execSQL("create table contactos(nombre text, telefono integer, direccion text, email text)"); } @Override public void onUpgrade(SQLiteDatabase db, int versionAnte, int versionNue) { db.execSQL("drop table if exists contactos"); db.execSQL("create table contactos(nombre text, telefono integer, direccion text, email text)"); } } Activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content"

Page 10: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="Nombre" /> <EditText android:id="@+id/etnombre" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:ems="10" > <requestFocus /> </EditText> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/etnombre" android:text="Telefono" />

<EditText android:id="@+id/ettelefono" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:ems="10" />

<TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/ettelefono" android:text="Direccion" />

<EditText android:id="@+id/etdireccion" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView3" android:ems="10" />

<TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/etdireccion" android:text="Email" />

<EditText android:id="@+id/etemail" android:layout_width="wrap_content"

Page 11: Agendapersonal

UNIVERSIDAD TECNONOLÓGICA DEL ESTADO DE ZACATECAS UNIDAD ACADÉMICA DE PINOS

TECNOLOGÍAS DE LA INFORMACIÓN Y COMUNICACIÓN

android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView4" android:ems="10" /> <Button android:id="@+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/button1" android:layout_marginTop="14dp" android:onClick="baja" android:text="Eliminar " />

<Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/etemail" android:layout_toRightOf="@+id/textView1" android:onClick="alta" android:text="Guardar " />

<Button android:id="@+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/button2" android:layout_alignParentRight="true" android:layout_marginRight="26dp" android:onClick="modificacion" android:text="Modificar " />

<Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignTop="@+id/button3" android:layout_marginLeft="28dp" android:layout_marginTop="16dp" android:layout_toRightOf="@+id/button3" android:onClick="consulta" android:text="Consultar " /> </RelativeLayout>