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 android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
@@ -38,21 +39,49 @@ public class AddProductoActivity extends AppCompatActivity {
private View.OnClickListener onSave() {
return new View.OnClickListener() {
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(
nombreText.getText().toString(),
Integer.valueOf(cantidadText.getText().toString()),
Integer.valueOf(precioText.getText().toString())
nombre,
Integer.valueOf(cantidad),
Integer.valueOf(precio)
);
Toast.makeText(
AddProductoActivity.this,
"Datos grabados!",
Toast.LENGTH_LONG
).show();
showToast("Datos grabados!");
finish();
}
};
}
private void showToast(String message) {
Toast.makeText(
AddProductoActivity.this,
message,
Toast.LENGTH_LONG
).show();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {

View File

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

View File

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