Saving data from the checkbox and sending to another Activity

Asked

Viewed 377 times

1

I have a list of products, which comes from an online database, each product contains a checkbox, name and value. What would be the best way to pass only the data of the marked products to a new Activity? It is a system similar to a shopping cart. I did not want to do with Sqlite, wanted something that was simpler. Can anyone help?

Data is saved on here:

private final List<String> idSelecionados = new ArrayList<>();

The code below represents the checkbox.

NOTE: The data would be sent to an Activity called "Details" that has only one listview...

CheckBox checMarcado  = (CheckBox) convertView.findViewById(R.id.checkBox);        

    checMarcado.setChecked(s.isMarcado());
    //Define uma tag para o checBox, para recuperar os dados quando for clicado no checkBox
    checMarcado.setTag(s);

    checMarcado.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {

        CheckBox check = (CheckBox) v;

        Serializando p = (Serializando) v.getTag();
        p.setMarcado(((CheckBox) v).isChecked());

        if (check.isChecked()) {
            Toast.makeText(context, "Adicionado " + p.getQuantidade()+ " - " + p.getNome(), Toast.LENGTH_SHORT).show();
            //Faz uma checagem se existe o mesmo valor na lista de inteiros
            if(!idSelecionados.contains(p.getNome())){
                //Adiciona em uma lista para poder manipular os dados depois
                idSelecionados.add(p.getNome() + p.getQuantidade());


            }

        } else {
            Toast.makeText(context, "Desmarcado " + p.getNome(), Toast.LENGTH_SHORT).show();
            //Faz uma checagem se existe o mesmo valor na lista de inteiros
            if(!idSelecionados.contains(p.getNome())){
                //Remove da lista se existir na lista
                idSelecionados.remove(p.getNome() + p.getQuantidade());
            }

        }

    }
});
  • tried something with Intent?

  • Post your code, base on it we can give you an answer!

  • I edited with the checkbox code... I tried with Intent yes, but I could not call in the list of Activity of Details...

1 answer

1

Solved... I passed as follows via Intent: (I don’t know if it was the right one, but for now, it was one that worked well for me)

Sending:

                    Intent i = new Intent(getContext(), Detalhes.class);
                    i.putExtra("lista", dados);
                    context.startActivity(i);

And to receive I did so:

    ArrayList<String> dados = getIntent().getStringArrayListExtra("lista");

However, if anyone knows a more suitable way, I would like to see... VLW...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.