使用bundle将值从活动传递到适配器

时间:2018-05-28 19:05:41

标签: android

我正在尝试传递一些值以显示在我的适配器中。但我不知道如何在适配器上接收值。

这是我的代码。

    confirmar_pedido.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Upload up = new Upload();
            Intent intent = new Intent(PedidoDetalhesActivity.this, CarrinhoItemPedidoAdapter.class);
            intent.putExtra("idProd", up.getmKey().toString().trim());
            intent.putExtra("nomeProd", value);
            intent.putExtra("preco", convertendo);
            startActivity(intent);
        }
    });

我如何在适配器中收到?

这里我会收到数据

public class CarrinhoItemPedidoAdapter extends RecyclerView.Adapter<CarrinhoItemPedidoAdapter.CarrinhoViewHolder> {

    private Context mContextCarrinho;
    private List<Pedidos> mUploadsPedidoCliente;
    String passado;
    DatabaseReference mDatabase;

    public CarrinhoItemPedidoAdapter(Context context, List<Pedidos> uploadsClientes) {
        mContextCarrinho = context;
        mUploadsPedidoCliente = uploadsClientes;
    }

    @Override
    public CarrinhoViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(mContextCarrinho).inflate(R.layout.carrinho_item, parent, false);
        return new CarrinhoViewHolder(v);
    }

    @Override
    public void onBindViewHolder(CarrinhoViewHolder holder, final int position) {
        final Pedidos pedCli = mUploadsPedidoCliente.get(position);


        //  holder.text_nome_item_pedido.setText(pedCli.getpedidoNmero() + pedCli.getQuantidade() + pedCli.getDescricao() + pedCli.getValor());
        //holder.text_nome_item_pedido.setText(" " + pedCli.getQuantidade() + " " + pedCli.getDescricao() + "                                          R$: " + pedCli.getValor());
        holder.text_nome_item_pedido.setText(" " + pedCli.getQuantity() + " " + pedCli.getDescricao());
        holder.text_valor_pedido.setText("R$: " + pedCli.getPrice());


    }

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

    public class CarrinhoViewHolder extends RecyclerView.ViewHolder {
        public TextView text_nome_item_pedido, text_valor_pedido, text_id_pedido;
        public Button btn_excluir_item_carrinho;

        public CarrinhoViewHolder(View itemView) {

            super(itemView);
            text_valor_pedido = itemView.findViewById(R.id.text_valor_pedido);
            text_nome_item_pedido = itemView.findViewById(R.id.text_nome_item_pedido);
            text_id_pedido = itemView.findViewById(R.id.text_id_pedido);
        }
    }
}

1 个答案:

答案 0 :(得分:0)

要从意图中恢复价值,只需使用以下内容:

Intent intent = getIntent();
string idprod = intent.getString("idProd");
int preco = intent.getInt("preco");

取决于值,如果它是字符串或int或parcelable。

点击此处了解更多信息: https://developer.android.com/training/basics/firstapp/starting-activity