I'm trying to implement a counter of items, i have 2 buttons ( + and -) and in the middle of those 2 i have a textView to check the quantity while increasing or lowering it, but since this is a textView it should let me introduce the quantity too, for example, if i click the textView i want to be able to put the value by myself, but when in introduce the value and Click Ok, it restarts to 1 which is the default value. Another thing is, if I increase the value using the (+ and -) buttons when i click the textView it reset to 1 and it doesn't save the value that i increase using the buttons.
XML Code:
<EditText
android:id="@+id/col_etSetQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:text="1"
android:textSize="14dip"
android:textStyle="bold"
android:saveEnabled="false"
android:inputType="number"
/>
And this is my adapter for this View:
final EditText edQuantity = (EditText) convertView.findViewById(R.id.col_etSetQuantity);
int quantity = items.get(position).getQuantity();
edQuantity.setText(String.valueOf(quantity));
final TextView tvTotal = (TextView) convertView.findViewById(R.id.col_priceTotal);
double total = cost * quantity;
String formattedTotal;
if(total == 0) {
formattedTotal = "Total: " + Constants.DOM_CURRENCY + "0";
} else {
formattedTotal = String.format("%,.2f", total);
formattedTotal = "Total: " + Constants.DOM_CURRENCY + formattedTotal;
}
tvTotal.setText(formattedTotal);
Button btLower = (Button) convertView.findViewById(R.id.col_btnLowerQuantity);
Button btIncrease = (Button) convertView.findViewById(R.id.col_btnIncreaseQuantity);
itemsEdited.get(position).setQuantity(quantity);
itemsEdited.get(position).setAmount(total);
btIncrease.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quantity = increaseQuantity(position, edQuantity);
itemsEdited.get(position).setQuantity(quantity);
double total = setTotalPrice(tvTotal,
products.get(position).getUnitPrice(), quantity);
itemsEdited.get(position).setAmount(total);
}
});
btLower.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int quantity = lowerQuantity(position, edQuantity);
itemsEdited.get(position).setQuantity(quantity);
double total = setTotalPrice(tvTotal,
products.get(position).getUnitPrice(), quantity);
itemsEdited.get(position).setAmount(total);
}
});
return convertView;
}
/**
* Se ejecuta cuando se presiona el botón del (-), para disminuir la cantidad de un producto
*/
public int lowerQuantity(int position, EditText setQuanitityBtn) {
Integer currentQuantity = Integer.parseInt(setQuanitityBtn.getText().toString());
int newQuantity = 1;
if(currentQuantity > 1)
{
newQuantity = currentQuantity - 1;
setQuanitityBtn.setText(String.valueOf(newQuantity));
}
return newQuantity;
}
/**
* Se ejecuta cuando se presiona el botón del (+), para aumentar la cantidad de un producto
*/
public int increaseQuantity(int position, EditText setQuanitityBtn) {
Integer currentQuantity = Integer.parseInt(setQuanitityBtn.getText().toString());
int newQuantity = 1;
if(currentQuantity < products.get(position).getQuantityOnHand())
{
newQuantity = currentQuantity + 1;
setQuanitityBtn.setText(String.valueOf(newQuantity));
} else {
Toast t = Toast.makeText(context, "Ya ha seleccionado la cantidad máxima en existencia", Toast.LENGTH_SHORT);
t.show();
}
return newQuantity;
}
public double setTotalPrice(TextView column, double cost, int quantity) {
double total = cost * quantity;
String formattedTotal = String.format("%,.2f", total);
formattedTotal = "Total: " + Constants.DOM_CURRENCY + formattedTotal;
column.setText(formattedTotal);
return total;
}
}