我正在从 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/