· 9 years ago · Dec 30, 2016, 02:26 PM
1public class AgregarPersona extends Activity implements OnClickListener {
2 EditText et;
3 EditText et2;
4 Button btnAgregar, read_bt;
5 SQLControlador dbconeccion;
6
7 @Override
8 protected void onCreate(Bundle savedInstanceState) {
9 // TODO Auto-generated method stub
10 super.onCreate(savedInstanceState);
11 setContentView(R.layout.agregar_persona);
12 et = (EditText) findViewById(R.id.et_nombre_id);
13 et2 = (EditText) findViewById(R.id.et_fecha_id);
14 btnAgregar = (Button) findViewById(R.id.btnAgregarId);
15
16 dbconeccion = new SQLControlador(this);
17 dbconeccion.abrirBaseDeDatos();
18 btnAgregar.setOnClickListener(this);
19 }
20
21 @Override
22 public void onClick(View v) {
23 // TODO Auto-generated method stub
24 switch (v.getId()) {
25 case R.id.btnAgregarId:
26 String name = et.getText().toString();
27 dbconeccion.insertarDatos(name);
28 Intent main = new Intent(AgregarPersona.this, MyActivity.class)
29 .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
30 startActivity(main);
31 break;
32
33 default:
34 break;
35 }
36 }
37}
38
39public class DBhelper extends SQLiteOpenHelper {
40
41 // Información de la tabla
42 public static final String TABLE_MEMBER = "personas";
43 public static final String PERSONA_ID = "_id";
44 public static final String PERSONA_NOMBRE = "nombre";
45 public static final String PERSONA_FECHA = "fecha";
46
47 // información del a base de datos
48 static final String DB_NAME = "DBPERSONAS";
49 static final int DB_VERSION = 1;
50
51 // Información de la base de datos
52 private static final String CREATE_TABLE = "create table "
53 + TABLE_MEMBER + "(" + PERSONA_ID
54 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
55 + PERSONA_NOMBRE + " TEXT NOT NULL, "
56 + PERSONA_FECHA + " TEXT NOT NULL);";
57
58 public DBhelper(Context context) {
59 super(context, DB_NAME, null,DB_VERSION);
60 }
61
62 @Override
63 public void onCreate(SQLiteDatabase db) {
64 db.execSQL(CREATE_TABLE);
65 }
66
67 @Override
68 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
69 // TODO Auto-generated method stub
70 db.execSQL("DROP TABLE IF EXISTS " + TABLE_MEMBER);
71 onCreate(db);
72 }
73}
74
75public class ModificarPersona extends Activity implements OnClickListener {
76
77 EditText et;
78 EditText et2;
79 Button btnActualizar, btnEliminar;
80
81 long member_id;
82
83 SQLControlador dbcon;
84
85 @Override
86 protected void onCreate(Bundle savedInstanceState) {
87 // TODO Auto-generated method stub
88 super.onCreate(savedInstanceState);
89 setContentView(R.layout.modificar_persona);
90
91 dbcon = new SQLControlador(this);
92 dbcon.abrirBaseDeDatos();
93
94 et = (EditText) findViewById(R.id.et_persona_id);
95 et2 = (EditText) findViewById(R.id.et_fecha_id);
96 btnActualizar = (Button) findViewById(R.id.btnActualizar);
97 btnEliminar = (Button) findViewById(R.id.btnEliminar);
98
99 Intent i = getIntent();
100 String memberID = i.getStringExtra("personaId");
101 String memberName = i.getStringExtra("personaNombre");
102 String memberFecha = i.getStringExtra("personaFecha");
103
104 member_id = Long.parseLong(memberID);
105
106 et.setText(memberName);
107 et2.setText(memberFecha);
108
109 btnActualizar.setOnClickListener(this);
110 btnEliminar.setOnClickListener(this);
111
112 }
113
114 @Override
115 public void onClick(View v) {
116 // TODO Auto-generated method stub
117 switch (v.getId()) {
118 case R.id.btnActualizar:
119 String memName_upd = et.getText().toString();
120 String memFecha_upd = et2.getText().toString();
121 dbcon.actualizarDatos(member_id, memName_upd, memFecha_upd);
122 this.returnHome();
123 break;
124
125 case R.id.btnEliminar:
126 dbcon.deleteData(member_id);
127 this.returnHome();
128 break;
129 }
130 }
131
132 public void returnHome() {
133
134 Intent home_intent = new Intent(getApplicationContext(),
135 MyActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
136
137 startActivity(home_intent);
138 }
139}
140
141public class MyActivity extends ActionBarActivity {
142
143 Button btnAgregarPersona;
144 ListView lista;
145 SQLControlador dbconeccion;
146 TextView tv_miemID, tv_miemNombre, tv_miemFecha;
147
148 @Override
149 protected void onCreate(Bundle savedInstanceState) {
150 super.onCreate(savedInstanceState);
151 setContentView(R.layout.activity_principal);
152
153 dbconeccion = new SQLControlador(this);
154 dbconeccion.abrirBaseDeDatos();
155 btnAgregarPersona = (Button) findViewById(R.id.btnAgregarPersona);
156 lista = (ListView) findViewById(R.id.listViewPersonas);
157
158 //acción del boton agregar persona
159 btnAgregarPersona.setOnClickListener(new OnClickListener() {
160 @Override
161 public void onClick(View v) {
162 Intent iagregar = new Intent(MyActivity.this, AgregarPersona.class);
163 startActivity(iagregar);
164 }
165 });
166
167 Cursor cursor = dbconeccion.leerDatos();
168
169 String[] from = new String[] {
170 DBhelper.PERSONA_ID,
171 DBhelper.PERSONA_NOMBRE,
172 DBhelper.PERSONA_FECHA
173 };
174 int[] to = new int[] {
175 R.id.persona_id,
176 R.id.persona_nombre,
177 R.id.persona_fecha
178 };
179
180 SimpleCursorAdapter adapter = new SimpleCursorAdapter(
181 MyActivity.this, R.layout.formato_fila, cursor, from, to);
182
183 adapter.notifyDataSetChanged();
184 lista.setAdapter(adapter);
185
186 // acción cuando hacemos click en item para poder modificarlo o eliminarlo
187 lista.setOnItemClickListener(new OnItemClickListener() {
188 @Override
189 public void onItemClick(AdapterView parent, View view, int position, long id) {
190
191 tv_miemID = (TextView) view.findViewById(R.id.persona_id);
192 tv_miemNombre = (TextView) view.findViewById(R.id.persona_nombre);
193 tv_miemFecha = (TextView) view.findViewById(R.id.persona_fecha);
194
195 String aux_personaId = tv_miemID.getText().toString();
196 String aux_personaNombre = tv_miemNombre.getText().toString();
197 String aux_personaFecha = tv_miemFecha.getText().toString();
198
199 Intent modify_intent = new Intent(getApplicationContext(), ModificarPersona.class);
200 modify_intent.putExtra("personaId", aux_personaId);
201 modify_intent.putExtra("personaNombre", aux_personaNombre);
202 modify_intent.putExtra("personaFecha", aux_personaFecha);
203 startActivity(modify_intent);
204 }
205 });
206 } //termina el onCreate
207} //termina clase
208
209public class SQLControlador {
210
211 private DBhelper dbhelper;
212 private Context ourcontext;
213 private SQLiteDatabase database;
214
215 public SQLControlador(Context c) {
216 ourcontext = c;
217 }
218
219 public SQLControlador abrirBaseDeDatos() throws SQLException {
220 dbhelper = new DBhelper(ourcontext);
221 database = dbhelper.getWritableDatabase();
222 return this;
223 }
224
225 public void cerrar() {
226 dbhelper.close();
227 }
228
229 public void insertarDatos(String name) {
230 ContentValues cv = new ContentValues();
231 cv.put(DBhelper.PERSONA_NOMBRE, name);
232 cv.put(DBhelper.PERSONA_FECHA, name);
233 database.insert(DBhelper.TABLE_MEMBER, null, cv);
234 }
235
236 public Cursor leerDatos() {
237 String[] todasLasColumnas = new String[] {
238 DBhelper.PERSONA_ID,
239 DBhelper.PERSONA_NOMBRE,
240 DBhelper.PERSONA_FECHA
241 };
242 Cursor c = database.query(DBhelper.TABLE_MEMBER, todasLasColumnas, null,
243 null, null, null, null);
244 if (c != null) {
245 c.moveToFirst();
246 }
247 return c;
248 }
249
250 public int actualizarDatos(long memberID, String memberName, String memberFecha) {
251 ContentValues cvActualizar = new ContentValues();
252 cvActualizar.put(DBhelper.PERSONA_NOMBRE, memberName);
253 cvActualizar.put(DBhelper.PERSONA_FECHA, memberFecha);
254 int i = database.update(DBhelper.TABLE_MEMBER, cvActualizar,
255 DBhelper.PERSONA_ID + " = " + memberID, null);
256 return i;
257 }
258
259 public void deleteData(long memberID) {
260 database.delete(DBhelper.TABLE_MEMBER, DBhelper.PERSONA_ID + "="
261 + memberID, null);
262 }
263}
264
265<?xml version="1.0" encoding="utf-8"?>
266<LinearLayout
267 xmlns:android="http://schemas.android.com/apk/res/android"
268 android:layout_width="match_parent"
269 android:layout_height="match_parent"
270 android:orientation="vertical"
271 android:padding="10dp">
272
273 <Button
274 android:id="@+id/btnAgregarPersona"
275 android:layout_width="fill_parent"
276 android:layout_height="wrap_content"
277 android:text="Agregar Cumpleaños" />
278
279 <ListView
280 android:id="@+id/listViewPersonas"
281 android:layout_width="match_parent"
282 android:layout_height="wrap_content"
283 android:dividerHeight="2dp">
284 </ListView>
285
286</LinearLayout>
287
288<?xml version="1.0" encoding="utf-8"?>
289<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
290 android:layout_width="match_parent"
291 android:layout_height="match_parent"
292 android:orientation="vertical"
293 android:padding="20dp" >
294
295 <EditText
296 android:id="@+id/et_nombre_id"
297 android:layout_width="match_parent"
298 android:layout_height="wrap_content"
299 android:ems="10"
300 android:layout_marginTop="40dp"
301 android:hint="Nombre">
302
303 <requestFocus />
304 </EditText>
305
306 <EditText
307 android:id="@+id/et_fecha_id"
308 android:layout_width="match_parent"
309 android:layout_height="wrap_content"
310 android:ems="10"
311 android:layout_marginTop="40dp"
312 android:hint="Fecha">
313 </EditText>
314
315 <Button
316 android:id="@+id/btnAgregarId"
317 android:layout_width="wrap_content"
318 android:layout_height="wrap_content"
319 android:layout_gravity="center"
320 android:text="Agregar" />
321
322</LinearLayout>
323
324<?xml version="1.0" encoding="utf-8"?>
325<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
326 android:layout_width="match_parent"
327 android:layout_height="match_parent"
328 android:orientation="vertical" >
329
330 <TextView
331 android:id="@+id/persona_id"
332 android:layout_width="fill_parent"
333 android:layout_height="wrap_content"
334 android:textColor="#00000000" />
335
336 <LinearLayout
337 android:orientation="horizontal"
338 android:layout_width="match_parent"
339 android:layout_height="wrap_content">
340
341 <TextView
342 android:id="@+id/persona_nombre"
343 android:layout_width="fill_parent"
344 android:layout_height="wrap_content"
345 android:padding="15dp"
346 android:textSize="17sp"
347 android:textStyle="bold"
348 android:text="Nombre"
349 android:layout_weight="1" />
350
351 <TextView
352 android:id="@+id/persona_fecha"
353 android:layout_width="fill_parent"
354 android:layout_height="wrap_content"
355 android:padding="15dp"
356 android:textSize="17sp"
357 android:textStyle="bold"
358 android:text="Fecha"
359 android:layout_weight="1" />
360
361 </LinearLayout>
362
363 <View
364 android:layout_width="fill_parent"
365 android:layout_height="2dp"
366 android:background="#3f51b5"
367 android:id="@+id/separador1"
368 android:layout_marginTop="10dp" />
369
370
371</LinearLayout>
372
373<?xml version="1.0" encoding="utf-8"?>
374<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
375 android:layout_width="match_parent"
376 android:layout_height="match_parent"
377 android:orientation="vertical"
378 android:padding="20dp">
379
380 <EditText
381 android:id="@+id/et_persona_id"
382 android:layout_width="match_parent"
383 android:layout_height="wrap_content"
384 android:layout_marginTop="40dp"
385 android:ems="10" />
386
387 <EditText
388 android:id="@+id/et_fecha_id"
389 android:layout_width="match_parent"
390 android:layout_height="wrap_content"
391 android:layout_marginTop="40dp"
392 android:ems="10" />
393
394 <LinearLayout
395 android:layout_width="match_parent"
396 android:layout_height="match_parent"
397 android:orientation="horizontal"
398 android:gravity="center_horizontal">
399
400 <Button
401 android:id="@+id/btnActualizar"
402 android:layout_width="wrap_content"
403 android:layout_height="wrap_content"
404 android:text="Actualizar" />
405
406 <Button
407 android:id="@+id/btnEliminar"
408 android:layout_width="wrap_content"
409 android:layout_height="wrap_content"
410 android:text="Eliminar" />
411 </LinearLayout>
412
413</LinearLayout>
414
415public int actualizarDatos(long memberID, String memberName) {
416 ContentValues cvActualizar = new ContentValues();
417 cvActualizar.put(DBhelper.MIEMBRO_NOMBRE, memberName);
418 cvActualizar.put(DBhelper.MIEMBRO_FECHA, memberName);
419 int i = database.update(DBhelper.TABLE_MEMBER, cvActualizar,
420 DBhelper.MIEMBRO_ID + " = " + memberID, null);
421 return i;
422 }
423
424cvActualizar.put(DBhelper.MIEMBRO_NOMBRE, memberName);
425 cvActualizar.put(DBhelper.MIEMBRO_FECHA, memberName);