I want to make a calculation in a listview that contains two parameters, a string and a double. How could I get only the double? For this I have a spinner (with 5 parameters) that contains a remote BD that I get with an entity called Tbalimentos (getter and setter) and according to what is selected I add the data to a listview (it gets two parameters of the value of the spinner selected).
The goal is to subtract when the listview is removed.
In the spinner I obtain a numerical parameter by means of a json object of the Tbalimentos that I rest with a value that I obtain from the previous screen. For each selection of the spinner it is added to a listview but when any element of it is deleted it makes a subtraction with this element removed. spdesayuno1 is the spinner.
spdesayuno1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,int position, long id) {
if(position!=0){
Double calorias=frutasList.get(position-1).getCalorias();
String gastoT= getArguments()!=null ? getArguments().getString("gasto3"):"SIN DATOS";
double gastoT2 = Double.parseDouble(gastoT);//
System.out.println("PARAMETRO: " + gastoT2);
if(calconsumidas.equals(new Double(0))) {
calconsumidas = gastoT2 - calorias;
}
txtobjetivo.setText(gastoT2+" - ");
txtcalorias.setText(calorias+" = ");
txtcalconsumidas.setText(String.format("%.2f",calconsumidas));
//paso de parametros args.putString("gasto4",txtcalconsumidas.getText().toString());
fragment.setArguments(args);
listaview.add((frutasList.get(position - 1).getAlimento()));
System.out.println("ListView.size(): " + listaview.size());
adapter2.notifyDataSetChanged();
}else{
txtcalorias.setText("");
}
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Here I delete and where is the problem where list is the listview:
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(final AdapterView<?> adapterView, final View view, final int i, long l) {
final int posicion=i;
AlertDialog.Builder dialogo1 = new AlertDialog.Builder(FragmentoMenu.this.getActivity());
dialogo1.setTitle("Importante");
dialogo1.setMessage("¿ Elimina este alimento ?");
dialogo1.setCancelable(false);
dialogo1.setPositiveButton("Confirmar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogo1, int id) {
//PROBLEMA
Double calorias = frutasList.get(position - 1).getCalorias();//si coloco esto me REGRESA el valor de los elementos del spinner
calconsumidas = calconsumidas + calorias;
txtcalconsumidas.setText(String.format("%.2f", calconsumidas));
//paso de parametros a colacion desayuno
args.putString("gasto2", txtcalconsumidas.getText().toString());
fragment.setArguments(args);
listaview.remove(posicion);
adapter2.notifyDataSetChanged();
}
});
dialogo1.setNegativeButton("Cancelar", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialogo1, int id) {
}
});
dialogo1.show();
return false;
}
});