Pass product list to another Activity [duplicate]

1

Hello, I'm learning and I wanted your suggestion, in a simple way at the beginning, of how to make this transition of products to another Activity. The application I'm developing it loads a list of products from a WebService, so in the application it does not have product registration, just reading the available products and in doing this reading the products are already loaded in Firebase, where I also store the clients that can be registered in the application.   So when someone is going to create a new application, my idea is that it has three steps. The first is the choice of the customer that will order, then the product or products that will be ordered, and the last screen where all the information (customer, product and product information: total value ...) appears. In the choose products screen, it already loads the Firebase information, using FirebaseUI (maybe not the best way, but so far it worked well with the client and is very fast to load) the function that I use to load the RecyclerView of the products is this:

public void setupRecycler(){


    adapter = new FirebaseRecyclerAdapter<Produto, ProdutoHolder>(
            Produto.class,
            R.layout.recyclerview_items,
            ProdutoHolder.class,
            ref
    ) {
        @Override
        protected void populateViewHolder(final ProdutoHolder viewHolder, final Produto model, final int position) {
            viewHolder.setId(model.getId().toUpperCase());
            viewHolder.setNome(model.getNome());
            viewHolder.setDescricao(model.getDescricao());
            viewHolder.setTamanho(model.getTamanho());
            viewHolder.setQuantidade(model.getQuantidade());
            viewHolder.setValor(model.getValor());


            viewHolder.mView.setOnClickListener(new View.OnClickListener() {
                @Override

                public void onClick(View v) {
                    if (SystemClock.elapsedRealtime() - lastClickTime < 1500){
                        viewHolder.itemView.setBackgroundColor(Color.RED);

                        return;
                    }
                    lastClickTime = SystemClock.elapsedRealtime();
                    viewHolder.itemView.setBackgroundColor(Color.GREEN);

                    String idProduto = model.getId();
                    String nomeProduto = model.getNome();
                    String descricaoProduto = model.getDescricao();
                    String tamanhoProduto = model.getTamanho();
                    String quantidadeProduto = model.getQuantidade();
                    String valorProduto = model.getValor();




                }
            });




        }
    };

    recyclerView.setAdapter(adapter);
}

Photos of the steps: [REQUEST] [CHOOSE CUSTOMER] [CHOOSE PRODUCT] [ FINAL SCREEN (not yet completed)

This OnClick was used for testing, but it works cool so too, inside the adapter itself it can pick up the product information, but how can I pass all this information to another Activity, and if it is more than one product? I thought about using JSON, I did some tests, but it did not work, I'm pretty sure, since you do not need to change anything, just pass the product information, so maybe an Array already solves it. Well this is my doubt, It has always helped me to ask a question here, so Obrigado.

    
asked by anonymous 20.09.2017 / 22:27

2 answers

2

Use Seriazable or Parceable (most recommended), standard interfaces are one of the same Java and another standard Android to be able to serialize objects at runtime.

    
21.09.2017 / 12:41
1

Yes, an array resolves, you fill it with your variables in the adapter and then pass that array to the other activity in a bundle, something like this:

public List<String> minhasEscolhas = new ArrayList<String>();
// voce pode criar essa variável no inicio da activity mas dentro da declaração de classe
public void setupRecycler(){
    // ...
    @Override
    protected void populateViewHolder(final ProdutoHolder viewHolder, final Produto model, final int position) {
        // ...
        viewHolder.mView.setOnClickListener(new View.OnClickListener() {
            @Override

            public void onClick(View v) {
                if (SystemClock.elapsedRealtime() - lastClickTime < 1500){
                        viewHolder.itemView.setBackgroundColor(Color.RED);
                        return;
                    }
                    lastClickTime = SystemClock.elapsedRealtime();                        viewHolder.itemView.setBackgroundColor(Color.GREEN);

                    String idProduto = model.getId();
                    String nomeProduto = model.getNome();
                    String descricaoProduto = model.getDescricao();
                    String tamanhoProduto = model.getTamanho();
                    String quantidadeProduto = model.getQuantidade();
                    String valorProduto = model.getValor();

                    minhasEscolhas.Add(idProduto);
                    minhasEscolhas.Add(nomeProduto);
                    // ...
                    minhasEscolhas.Add(oQueVoceQiser);
                  }
            });
        }
    };
    recyclerView.setAdapter(adapter); 
}

Once the array is populated, you pass it to the intent of the activity you are going to call:

public void emUmEventoOnClickqueChamaActityQualquer() {
        Bundle data = new Bundle();
        data.putParcelableArrayList("produtos", minhasEscolhas);
        // aqui passa a lista para o bundle
        Intent intent = new Intent(this, ActivityQueQueroChamarComOArrayDeProdutos.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.putExtras(data);
        startActivity(intent);
}

Then in the oncreate of "ActivityQueQueroChamarComOrArray" you take the parameter passed by the bundle and distribute as you want in the activity:

// ...
private List<String> minhasEscolhas = new ArrayList<String>();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        minhasEscolhas = extras.getParcelableArrayList("produtos");
        // ...
    }
//...

By noting that when you declared the variable public List<String> minhasEscolhas = new ArrayList<String>(); in the parent activity, you can also get this variable in the child activity, but you'd better go through the bundle because so the source activty is released

I hope it helps!

    
21.09.2017 / 03:03