Validacion

This commit is contained in:
Daniel Cortés
2019-11-22 18:45:47 -03:00
parent 4340a85686
commit f72065ea58
3 changed files with 81 additions and 31 deletions

View File

@@ -3,6 +3,7 @@ package xyz.danielcortes.androidsqlite;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@@ -38,21 +39,49 @@ public class AddProductoActivity extends AppCompatActivity {
private View.OnClickListener onSave() { private View.OnClickListener onSave() {
return new View.OnClickListener() { return new View.OnClickListener() {
public void onClick(View v) { public void onClick(View v) {
String nombre = nombreText.getText().toString();
String cantidad = cantidadText.getText().toString();
String precio = precioText.getText().toString();
if (nombre == null || nombre.trim().isEmpty()) {
showToast("El nombre esta vacio");
return;
}
if (cantidad == null || cantidad.trim().isEmpty()) {
showToast("La cantidad esta vacia");
return;
} else if (!TextUtils.isDigitsOnly(cantidad)) {
showToast("La cantidad solo puede contener numeros");
return;
}
if(precio == null || cantidad.trim().isEmpty()) {
showToast("El precio esta vacio");
return;
} else if(!TextUtils.isDigitsOnly(precio)) {
showToast("El precio solo puede contener numeros");
return;
}
databaseHelper.addProducto( databaseHelper.addProducto(
nombreText.getText().toString(), nombre,
Integer.valueOf(cantidadText.getText().toString()), Integer.valueOf(cantidad),
Integer.valueOf(precioText.getText().toString()) Integer.valueOf(precio)
); );
Toast.makeText(
AddProductoActivity.this, showToast("Datos grabados!");
"Datos grabados!",
Toast.LENGTH_LONG
).show();
finish(); finish();
} }
}; };
} }
private void showToast(String message) {
Toast.makeText(
AddProductoActivity.this,
message,
Toast.LENGTH_LONG
).show();
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) { if (item.getItemId() == android.R.id.home) {

View File

@@ -59,11 +59,13 @@ public class CustomAdapter extends BaseAdapter {
Producto producto = productos.get(i); Producto producto = productos.get(i);
viewHolder.nombreText.setText(producto.getNombre()); viewHolder.nombreText.setText(producto.getNombre());
viewHolder.cantidadText.setText("Cantidad: " + producto.getCantidad());
NumberFormat formatter = NumberFormat.getCurrencyInstance(); NumberFormat numberFormatter = NumberFormat.getNumberInstance();
viewHolder.precioText.setText(formatter.format(producto.getPrecio())); viewHolder.cantidadText.setText("Cantidad: " + numberFormatter.format(producto.getCantidad()));
viewHolder.totalText.setText(formatter.format(producto.getCantidad() * producto.getPrecio()));
NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
viewHolder.precioText.setText(currencyFormatter.format(producto.getPrecio()));
viewHolder.totalText.setText(currencyFormatter.format(producto.getCantidad() * producto.getPrecio()));
return view; return view;
} }

View File

@@ -5,6 +5,7 @@ import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog; import android.app.AlertDialog;
import android.content.DialogInterface; import android.content.DialogInterface;
import android.os.Bundle; import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
@@ -58,11 +59,7 @@ public class UpdateDeleteActivity extends AppCompatActivity {
@Override @Override
public void onClick(DialogInterface dialog, int which) { public void onClick(DialogInterface dialog, int which) {
databaseHelper.deleteProducto(producto.getId()); databaseHelper.deleteProducto(producto.getId());
Toast.makeText( showToast("Producto eliminado correctamente");
UpdateDeleteActivity.this,
"Usuario eliminado correctamente",
Toast.LENGTH_LONG
).show();
finish(); finish();
} }
}) })
@@ -77,15 +74,33 @@ public class UpdateDeleteActivity extends AppCompatActivity {
return new View.OnClickListener() { return new View.OnClickListener() {
@Override @Override
public void onClick(View v) { public void onClick(View v) {
if (producto.getNombre().equals(nombreText.getText().toString()) && String nombre = nombreText.getText().toString();
String.valueOf(producto.getPrecio()).equals(precioText.getText().toString()) && String cantidad = cantidadText.getText().toString();
String.valueOf(producto.getCantidad()).equals(cantidadText.getText().toString()) String precio = precioText.getText().toString();
) {
Toast.makeText( if (nombre == null || nombre.trim().isEmpty()) {
UpdateDeleteActivity.this, showToast("El nombre esta vacio");
"No hay nada que actualizar", return;
Toast.LENGTH_LONG }
).show(); if (cantidad == null || cantidad.trim().isEmpty()) {
showToast("La cantidad esta vacia");
return;
} else if (!TextUtils.isDigitsOnly(cantidad)) {
showToast("La cantidad solo puede contener numeros");
return;
}
if (precio == null || cantidad.trim().isEmpty()) {
showToast("El precio esta vacio");
return;
} else if (!TextUtils.isDigitsOnly(precio)) {
showToast("El precio solo puede contener numeros");
return;
}
if (producto.getNombre().equals(nombre) &&
String.valueOf(producto.getPrecio()).equals(precio) &&
String.valueOf(producto.getCantidad()).equals(cantidad)) {
showToast("No hay nada que actualizar");
return; return;
} }
@@ -95,17 +110,21 @@ public class UpdateDeleteActivity extends AppCompatActivity {
Integer.valueOf(cantidadText.getText().toString()), Integer.valueOf(cantidadText.getText().toString()),
Integer.valueOf(precioText.getText().toString()) Integer.valueOf(precioText.getText().toString())
); );
Toast.makeText(
UpdateDeleteActivity.this,
"Actualización realizada!",
Toast.LENGTH_LONG
).show();
showToast("Actualización realizada!");
finish(); finish();
} }
}; };
} }
private void showToast(String message) {
Toast.makeText(
UpdateDeleteActivity.this,
message,
Toast.LENGTH_LONG
).show();
}
@Override @Override
public boolean onOptionsItemSelected(MenuItem item) { public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) { if (item.getItemId() == android.R.id.home) {