How to get from a listview (a string of one or more parameters) a parameter IN ANDROID

0

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;
            }
        });
    
asked by Angelica 09.10.2017 в 15:45
source

1 answer

0

You could divide the information with ; and use split that returns a string array, but it would separate what is between ; . For example:

String[] datos = itemValue.split(";"); 

will generate a String array through the data that is between ; . In my case I'm doing university projects and I just want to get the id. in my list I have the data like this:

1;Juan Carlos;Bodoque 

When using split , the 1,Juan Carlos,Bodoque separates me, allowing me to access the value I need. For example the id then I only create one variable:

int id = Integer.parseInt(datos[0]);
    
answered by 10.10.2017 в 04:31