Passing data with Linearlayout onClick

Asked

Viewed 39 times

0

In the software that I am developing when client click on an item in my Recyclerview, I would like you to call another Activity showing the item photo, price, description and would have him choose the quantity. However, I have tried some methods, and when I click, it even forwards to the receiving activity, however, if the client press the back key and then select another item,.

Can someone help me with that?

NOTE: I am capturing the click using my Linearlayout

OBS2: I’m using Firebase

THE ADAPTER

public void onBindViewHolder(final ImageViewHolder holder, final int position) {
        final Upload uploadCurrent = mUploads.get(position);

    holder.textViewId.setText("ID: " + uploadCurrent.getmKey());
    holder.textViewName.setText("Nome: " + uploadCurrent.getName());
    holder.textViewDescricao.setText("Acompanhamento: " + uploadCurrent.getmDescricao());
    holder.textViewPreco.setText("Valor Unitário: R$ " + uploadCurrent.getmPreco());
    holder.linearLayoutProdutosCardapio.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mDatabaseRef = FirebaseDatabase.getInstance().getReference("pedidos");

            Upload task = new Upload(uploadCurrent.getmKey(), uploadCurrent.getName(), uploadCurrent.getmDescricao(), uploadCurrent.getmPreco(), uploadCurrent.getImageUrl());

            String uploadId = task.getmKey();


             mDatabaseRef.child(uploadId).setValue(task);



            Intent intent = new Intent(mContext, PedidoActivity.class);
            mContext.startActivity(intent);
        }
    });

CARDAPIO ACTIVITY WHERE THE CUSTOMER WILL SELECT THE PRODUCT

 mRecyclerView = findViewById(R.id.recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mProgressCircle = findViewById(R.id.progress_circle);

    mUploads = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");

    mDatabaseRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload upload = postSnapshot.getValue(Upload.class);
                mUploads.add(upload);
            }

            mAdapter = new ImagemAdapter(ImagemActivity.this, mUploads);

            mRecyclerView.setAdapter(mAdapter);
            mProgressCircle.setVisibility(View.INVISIBLE);


        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(ImagemActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            mProgressCircle.setVisibility(View.INVISIBLE);
        }
    });
}

THE ORDER ADAPTER

public void onBindViewHolder(final PedViewHolder holder, final int position) {
        final Upload upPedidos = mUploadsPedidos.get(position);
        mDataBasePedidos = FirebaseDatabase.getInstance().getReference("pedidos");

    holder.text_preco_item_pedido.setText("Valor Unitário:  R$ " + upPedidos.getmPreco());
    holder.text_descricao_item_pedido.setText(upPedidos.getmDescricao());

    DisplayMetrics displayMetrics = mContextPedido.getResources().getDisplayMetrics();

    final int height = (displayMetrics.heightPixels / 4);
    final int width = (displayMetrics.widthPixels / 2);

    Picasso.with(mContextPedido)
            .load(upPedidos.getImageUrl())
            .resize(width, height)
            .centerCrop()
            .into(holder.foto_pedido_carregado);

}

@Override
public int getItemCount() {
    return mUploadsPedidos.size();
}

public class PedViewHolder extends RecyclerView.ViewHolder {
    private LinearLayout linearLayoutPedidos;
    private ImageView foto_pedido_carregado;
    private TextView text_preco_item_pedido, text_descricao_item_pedido;

    public PedViewHolder(View itemView) {
        super(itemView);
        linearLayoutPedidos = itemView.findViewById(R.id.linearLayoutPedidos);
        foto_pedido_carregado = itemView.findViewById(R.id.foto_pedido_carregado);
        text_preco_item_pedido = itemView.findViewById(R.id.text_preco_item_pedido);
        text_descricao_item_pedido = itemView.findViewById(R.id.text_descricao_item_pedido);
    }
}
}

THE ACTIVITY OF THE APPLICATION

mRecyclerViewPedidos = findViewById(R.id.recycler_view_pedido);
        mRecyclerViewPedidos.setHasFixedSize(true);
        mRecyclerViewPedidos.setLayoutManager(new LinearLayoutManager(this));


    mProgressBarCirclePedido = findViewById(R.id.progress_circle_pedido);
    mUploadsPedidos = new ArrayList<>();

    mDataBasePedidos = FirebaseDatabase.getInstance().getReference("pedidos");


    mDataBasePedidos.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {

            for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                Upload up = postSnapshot.getValue(Upload.class);
                mUploadsPedidos.add(up);
            }
            mAdapterPedido = new PedidoAdapter(PedidoActivity.this, mUploadsPedidos);

            mRecyclerViewPedidos.setAdapter(mAdapterPedido);
            mProgressBarCirclePedido.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(PedidoActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
            mProgressBarCirclePedido.setVisibility(View.INVISIBLE);
        }
    });

}
}
  • Put your code in the question

  • If there is a receiving Activity, it should not receive data sent from Adapter with a Bundle via Intent?

No answers

Browser other questions tagged

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