Pass product list to another Activity

Asked

Viewed 1,278 times

1

Hello, I am learning and I wanted the suggestion of you, in a simple way at first, how to make this passage of products to another Activity. The application I’m developing it loads a list of products from a Webservice, so in the application there is no registration of products, just reading the products available and when doing this reading is already loaded the products in Firebase, where I also store customers who can be registered in the app. So when someone is going to create a new order, my idea is to have three steps. The first is the choice of the customer who will place the order, then the or products that will be ordered, and on the last screen where appears all the information (customer, product and product information: as total value...). On the screen of choosing the products it already loads the information from Firebase, using Firebaseui (Maybe not the best way, but so far it worked well with the customer and is very fast to load) the function 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 stages: [REQUEST] [CHOOSE CLIENT] [PICK PRODUCT] [FINAL SCREEN (not yet finished)

This Onclick was using it for testing, but it works cool that way too, inside the Adapter itself it can get the product information, but how can I pass all this information to another Activity, and if it’s more than one product? I thought about using JSON, I did some tests, but it didn’t work, I’m very doubtful, as you don’t need to change anything of the product, just pass the product information, so maybe an Array already solves. Well that’s my question, it’s always helped the times I ask a question here, so thank you.

2 answers

3


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

1

yes, an array resolves, Voce fills it with its variables in Adapter and then passes 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 has been filled in, Voce passes it to the Activity input Voce will 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 "Activityquequerochamarcomoarraydeproducts" oncreate Voce takes the parameter passed by Bundle and distributes as Voce you want in 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");
        // ...
    }
//...

Lenbrando who when declared the variable public List<String> minhasEscolhas = new ArrayList<String>(); in Activity father, Voce can also take this variable in Activity daughter, but it is better to pass through Bundle because thus the activty of origin is released

hope it helps!

  • This does not pass a Product Array but rather a String Array.

  • yes, it is true friend @ramaral, but he asked to clarify the doubt, and this the answer did, because regardless of the type of array that is going through, the transport process is the same, as it can fill a "hashmap" with the product, which will have to be shipped via Bundle the same way it is in the reply.

  • Only with the information given in the reply, if instead of ArrayList<String> is used ArrayList<Produto>, won’t work

  • if he has a Product class to pass as Type to Arraylist<Product> it will work the same way, but I based on the code he posted, hence where he passes the product values he passes a String for each data, ex.: String idProduto = model.getId();String nomeProduto = model.getNome(); ... and so on, so I responded as a string array

Browser other questions tagged

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