android - 通过 recyclerview 的 onClick 传递 Intent

标签 android android-intent android-recyclerview

我正在从 recyclerview 中卡的数据库 onClick() 中获取 id,并在下一个 Activity 中传递此 id,其中应显示与此 id 相关的详细信息。我得到的 ID 是错误的,因此会显示错误的详细信息。请帮忙。

 @Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
    ListInvoice listInvoice = listInvoices.get(position);

    holder.invoice_noText.setText(listInvoice.getInvoice_no());
    Picasso.with(context).load(listInvoice.getLogo()).resize(180,80).into(holder.imageText);
    holder.dateText.setText(listInvoice.getCreated_at());
    holder.cc_codeText.setText(listInvoice.getCc_code());
    holder.nameText.setText(listInvoice.getName());
    holder.test_priceText.setText(listInvoice.getTest_price());
    id = listInvoice.getId();
    holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            context = v.getContext();
           int a = holder.getAdapterPosition();
           passData(a);

        }
    });

}

private void passData(int a) {

    Intent intent = new Intent(context,InvoiceDetails.class);
    intent.putExtra("id", ""+id);
    context.startActivity(intent);
}

这就是我在下一个 Activity 中获取数据的方式

Intent intent = getIntent();
    id = intent.getStringExtra("id");
    Toast.makeText(getApplicationContext(),id,Toast.LENGTH_LONG).show();
    loadInvoice(id);

最佳答案

试试这个

holder.relativeLayout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
       id = listInvoice.getId();
       passData(id);

    }
});

private void passData(int id) {

    Intent intent = new Intent(context,InvoiceDetails.class);
    intent.putExtra("id", ""+id);
    context.startActivity(intent);
}

关于android - 通过 recyclerview 的 onClick 传递 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48001762/

相关文章:

Android RecyclerView - 在键盘打开时保持最后一个可见项目可见

java - OpenGL es 1.1 - android - gl.glDeleteTextures 是否释放视频内存?

android - 如何访问给定内容 URI 的目录中的文件?

android - 通知打开 Activity ,按后退按钮,防止打开后退堆栈 Activity ?

java - 将数据从 RecyclerView 发送到 Fragment

android - 如何使用数据绑定(bind)初始化 Recycler View 适配器上的点击监听器?

php - 使用 php 将值插入 mysql

android - 如何注册 OMX 内核以添加新的解码器

android - 与 Android Studio 相关的 build.gradle 文件中无法访问的几个任务

android - 另一个 "how to find className"