Contenido sobre Android
Buscar
Social
Ofertas laborales ES

Foro sobre Android > aplicacion con sqlite

//borrar.

import android.app.Activity;
import android.content.ContentValues;
import android.content.Entity.*;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.provider.ContactsContract.RawContacts.Entity;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class borrarAlumno extends Activity{
LinearLayout principal,secundario,secundario2;
TextView titulo,titulo2;
EditText matricula;
Button aceptar,volver;
sqlHelper sh;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
sh = new sqlHelper(this,"BDPRUEBA",null,1);
init_layout();
}
private void init_layout() {
// TODO Auto-generated method stub
this.principal = new LinearLayout(this);
this.principal.setOrientation(LinearLayout.VERTICAL);
this.secundario = new LinearLayout(this);
this.secundario.setOrientation(LinearLayout.HORIZONTAL);
this.secundario2 = new LinearLayout(this);
this.secundario2.setOrientation(LinearLayout.HORIZONTAL);

this.titulo = new TextView(this);
this.titulo.setText("Borrar alumno");
this.titulo.setPadding(60, 20, 10, 10);

this.principal.addView(titulo);

this.titulo2 = new TextView(this);
this.titulo2.setText("Matricula:");
this.secundario.addView(titulo2);

this.matricula = new EditText(this);
this.matricula.setHint("Ingrese matricula alumno");
this.secundario.addView(matricula);

this.principal.addView(secundario);

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(80, 30);
this.aceptar = new Button(this);
this.volver = new Button(this);

this.secundario2.setPadding(40, 20, 5, 20);

this.aceptar.setText("Aceptar");
this.aceptar.setLayoutParams(p1);
this.secundario2.addView(aceptar);


this.volver.setText("Volver");
this.volver.setLayoutParams(p1);
this.secundario2.addView(volver);

this.principal.addView(secundario2);

setContentView(this.principal);

evento();

}
private void evento() {
// TODO Auto-generated method stub
this.volver.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(borrarAlumno.this, menuActivity.class);
startActivity(i);
finish();
}
});
this.aceptar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String mat = matricula.getText().toString().trim();
int m = Integer.parseInt(mat);

db = sh.getWritableDatabase();
if (db!=null){
db.delete("estudiantes", "matricula="+m+"", null);
Toast.makeText(aceptar.getContext(),"Borrado con exito" + m,Toast.LENGTH_SHORT).show();
matricula.setText("");
}
}
});
}

}


//ingresar
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.database.sqlite.SQLiteDatabase;

public class ingresarAlumno extends Activity {
LinearLayout lprincipal,lsecundario;
EditText nombre,apellido,institucion,matricula;
TextView titulo,titulo2,titulo3,titulo4,titulo5,titulo6;
//ImageButton grabar;
Button grabar,volver;
sqlHelper sh;
SQLiteDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

sh = new sqlHelper(this,"BDPRUEBA",null,1);


init_menu();


}


private void init_menu() {
// TODO Auto-generated method stub

this.lprincipal = new LinearLayout(this);
this.lsecundario = new LinearLayout(this);
this.lprincipal.setOrientation(LinearLayout.VERTICAL);
this.lsecundario.setOrientation(LinearLayout.HORIZONTAL);

LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
this.lprincipal.setPadding(20, 20, 10, 10);
this.lprincipal.setLayoutParams(p);

this.titulo = new TextView(this);
this.titulo.setText("Nombre:");
this.lprincipal.addView(titulo);

//CUANDO QUIERO QUE QUEDE TODO LO DEMAS DE ESPACIO LE AGREGO EL LAYOUTPARAMS SOLO AL ULTIMO OBJETO
this.nombre = new EditText(this);
this.nombre.setLayoutParams(p);
this.nombre.setHint("Escribir nombre");
this.lprincipal.addView(nombre);


this.titulo2 = new TextView(this);
this.titulo2.setText("Apellidos:");
this.lprincipal.addView(titulo2);

this.apellido = new EditText(this);
this.apellido.setHint("Escibir apellido");
this.apellido.setLayoutParams(p);
this.lprincipal.addView(apellido);


this.titulo3 = new TextView(this);
this.titulo3.setText("Institucion:");
this.lprincipal.addView(titulo3);

this.institucion = new EditText(this);
this.institucion.setHint("Escribir institucion");
this.institucion.setLayoutParams(p);
this.lprincipal.addView(institucion);

this.titulo4 = new TextView(this);
this.titulo4.setText("Matricula:");
this.lprincipal.addView(titulo4);

this.matricula = new EditText(this);
this.matricula.setHint("Escribir matricula");
this.matricula.setLayoutParams(p);
this.lprincipal.addView(matricula);

LinearLayout.LayoutParams p1 = new LinearLayout.LayoutParams(80, 30);

this.grabar = new Button(this);
//this.grabar.setLayoutParams(p1);
this.grabar.setText("grabar");
this.grabar.setLayoutParams(p1);
this.lsecundario.addView(grabar);
this.lsecundario.setPadding(20, 20, 10, 10);

this.volver = new Button(this);
this.volver.setText("volver");
this.volver.setLayoutParams(p1);
this.lsecundario.addView(volver);


this.lprincipal.addView(lsecundario);


setContentView(this.lprincipal);

evento();
}
private void evento() {

this.volver.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(ingresarAlumno.this, menuActivity.class);
startActivity(i);
finish();
}
});
// TODO Auto-generated method stub
this.grabar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

db= sh.getWritableDatabase();
String aux_nombre = ingresarAlumno.this.nombre.getText().toString().trim();
String aux_apellido = ingresarAlumno.this.apellido.getText().toString().trim();
String aux_institucion = ingresarAlumno.this.institucion.getText().toString().trim();
String aux_matricula = ingresarAlumno.this.matricula.getText().toString().trim();
Integer x = Integer.parseInt(aux_matricula);


if (db!= null)
{

ContentValues nRegistro = new ContentValues();
nRegistro.put("matricula", x);
nRegistro.put("nombre",aux_nombre );
nRegistro.put("apellido", aux_apellido);
nRegistro.put("institucion",aux_institucion );

db.insert("estudiantes", null, nRegistro);

Toast.makeText(grabar.getContext(),"Grabacion exitosa",Toast.LENGTH_SHORT).show();
nombre.setText("");
apellido.setText("");
institucion.setText("");
matricula.setText("");
db.close();
}
else
{
Toast.makeText(grabar.getContext(),"Error",Toast.LENGTH_SHORT).show();
}

}
});

}
}


//modifica
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class modificaAlumno extends Activity{
Spinner datos;
Button volver,grabar;
EditText date,matricula,tipo;
sqlHelper sh;
SQLiteDatabase db;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.modificar);
sh = new sqlHelper(this,"BDPRUEBA",null,1);
init_layout();
}

private void init_layout() {
// TODO Auto-generated method stub
grabar = (Button)findViewById(R.id.btnguardar);
date =(EditText)findViewById(R.id.editNuevo);
matricula = (EditText)findViewById(R.id.editMatricula);
tipo = (EditText)findViewById(R.id.editTipo);
db = sh.getReadableDatabase();


grabar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
String tipos = tipo.getText().toString().trim();
String matricu = matricula.getText().toString().trim();
String nuevo = date.getText().toString().trim();
int mat = Integer.parseInt(matricu);
// TODO Auto-generated method stub
if (tipos.equals("nombre"))
{

ContentValues val = new ContentValues();
val.put("matricula", mat);
val.put("nombre", nuevo);

db.update("estudiantes", val, "matricula=" + mat, null);

Toast.makeText(grabar.getContext(),"Nombre actualizdo ",Toast.LENGTH_SHORT).show();
tipo.setText("");
matricula.setText("");
date.setText("");

db.close();
}
if (tipos.equals("apellido"))
{


ContentValues val = new ContentValues();
val.put("matricula", mat);
val.put("apellido", nuevo);

db.update("estudiantes", val, "matricula=" + mat, null);

Toast.makeText(grabar.getContext(),"Apellido actualizado ",Toast.LENGTH_SHORT).show();
tipo.setText("");
matricula.setText("");
date.setText("");

}
if (tipos.equals("institucion"))
{

ContentValues val = new ContentValues();
val.put("matricula", mat);
val.put("institucion", nuevo);

db.update("estudiantes", val, "matricula=" + mat, null);

Toast.makeText(grabar.getContext(),"Institucion actualizada ",Toast.LENGTH_SHORT).show();
tipo.setText("");
matricula.setText("");
date.setText("");

}

}
});


evento();
}

private void evento() {
// TODO Auto-generated method stub
volver = (Button)findViewById(R.id.btnvolver);

volver.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(modificaAlumno.this, menuActivity.class);
startActivity(i);
finish();
}
});
}



}

//menu
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;


public class menuActivity extends Activity {
/** Called when the activity is first created. */
ImageButton registrar,modifica,borrar,mostrar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

registrar = (ImageButton)findViewById(R.id.imgregistro);

registrar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

Intent i = new Intent();
i.setClass(menuActivity.this, ingresarAlumno.class);
startActivity(i);
finish();
}
});

modifica = (ImageButton)findViewById(R.id.actualiza);

modifica.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(menuActivity.this, modificaAlumno.class);
startActivity(i);
finish();

}
});

borrar = (ImageButton)findViewById(R.id.imgborrar);

borrar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(menuActivity.this, borrarAlumno.class);
startActivity(i);
finish();
}
});

mostrar = (ImageButton)findViewById(R.id.imgMostrar);

mostrar.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(menuActivity.this, mostrarAlumno.class);
startActivity(i);
finish();
}
});
}


}


//mostrar
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class mostrarAlumno extends Activity {
TextView texto;
Button volver;
sqlHelper sh;
SQLiteDatabase db;


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.mostrar);
sh = new sqlHelper(this,"BDPRUEBA",null,1);
db = sh.getReadableDatabase();

texto = (TextView)findViewById(R.id.txtDatos);
List<String>list = new ArrayList<String>();
StringBuilder sb = new StringBuilder();

int i = 0;
Cursor c = db.query("estudiantes", new String[]{"matricula,nombre,apellido,institucion"}, null, null,null,null,"nombre desc");

String [] ins = new String[1];
ins[0]= "ciisa";
// Cursor c = db.rawQuery("SELECT matricula, nombre , apellido , institucion FROM estudiantes WHERE institucion ='ciisa'",null);

if (c.moveToFirst())
{

do{
i++;
int codigo = c.getInt(0);
String nombre = c.getString(1);
String apellido = c.getString(2);
String institucion = c.getString(3);
String cod = String.valueOf(codigo);
list.add("Matricula: " + cod + "\n Nombre alumno: " + nombre + "\n Apellido: " + apellido + "\n Institucion " + institucion + "\n-_-_-_-_-_-_-_--_--_--_--_--");

}while(c.moveToNext());
if (c != null && !c.isClosed()) {
c.close();
}
}
for(String name:list){
sb.append(name);
sb.append("\n");
}
db.close();

volver = (Button)findViewById(R.id.btnVolver);

volver.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent();
i.setClass(mostrarAlumno.this, menuActivity.class);
startActivity(i);
finish();
}
});

texto.setText(sb.toString());



}



}

//clase sqlhelper
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;

public class sqlHelper extends SQLiteOpenHelper{

String tblEstudio = "CREATE TABLE estudiantes (matricula INTEGER PRIMARY KEY , nombre TEXT , apellido TEXT , institucion TEXT)";

public sqlHelper(Context contexto , String nombre, CursorFactory factory, int version) {
super(contexto, nombre, factory, version);
// TODO Auto-generated constructor stub
}

@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL(tblEstudio);
}

@Override
public void onUpgrade(SQLiteDatabase db, int versionAnterior, int versionNueva) {
// TODO Auto-generated method stub

db.execSQL("DROP TABLE IF EXISTS estudiantes ");

db.execSQL(tblEstudio);
}

}


//modificar xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp">

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Que desea modificar"
android:layout_gravity="center"
android:padding="10dp"
></TextView>

<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nombre - Apellido - Institucion"
android:layout_gravity="center"
/>

<EditText
android:id="@+id/editTipo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ingrese que desea modificar"/>

<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Matricula"
android:layout_gravity="center"
android:padding="5dp"/>

<EditText
android:id="@+id/editMatricula"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ingrese matricula"
/>

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dato"
android:layout_gravity="center"
android:padding="10dp"></TextView>

<EditText
android:id="@+id/editNuevo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ingrese dato nuevo">

<requestFocus />
</EditText>

<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="10dp">

<Button
android:id="@+id/btnguardar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Guardar"
android:layout_marginLeft="50dp"
/>

<Button
android:id="@+id/btnvolver"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Volver" />

</LinearLayout>

</LinearLayout>


//menu en xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Menu"
android:layout_gravity="center"
android:gravity="center"
android:padding="10dp"
>
</TextView>

<LinearLayout
android:id="@+id/layoutmenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >

<TextView
android:id="@+id/ingrese"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Ingresar alumno" >
</TextView>

<ImageButton
android:id="@+id/imgregistro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/registro" >

</ImageButton>
<TextView
android:id="@+id/actualizar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Actualizar alumno" >
</TextView>
<ImageButton
android:id="@+id/actualiza"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/actualiza" >
</ImageButton>
<TextView
android:id="@+id/borrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Borrar alumno" >
</TextView>
<ImageButton
android:id="@+id/imgborrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/borrar"
>
</ImageButton>
<TextView
android:id="@+id/mostrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Mostrar alumno" >
</TextView>
<ImageButton
android:id="@+id/imgMostrar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="@drawable/mostrar" >
</ImageButton>
</LinearLayout>

</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cl.ciisa.rq"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="8" />




<category android:name="android.intent.category.LAUNCHER" />










</manifest>

noviembre 11, 2011 | Unregistered Commenterrquiroz