我必须在客户点击cardview时获取详细信息,但未在我的活动中显示数据。
这是我的代码:
public void onBindViewHolder(MenuItemViewHolder holder, int position) {
final int _index = position;
holder.iBookingRef.setText(itemList.get(position).getBookingID());
holder.iClientName.setText(itemList.get(position).getDelegeteName());
holder.iHotelName.setText(itemList.get(position).getHotelName());
holder.layout.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(context, ArrivalsActivity.class);
intent.putExtra("BookingID", itemList.get(_index).getBookingID());
intent.putExtra("DelegeteName", itemList.get(_index).getDelegeteName());
intent.putExtra("HotelName", itemList.get(_index).getHotelName());
context.startActivity(intent);
}
});
}
答案 0 :(得分:1)
在包含方法的适配器中创建一个接口。在实现适配器时,这些方法将在您的活动中实现,您可以执行您想要的任何操作。
public class Adapter extends RecyclerView.Adapter<MyRecycleViewHolder> {
public interface Callbacks {
public void onButtonClicked(String titleKey);
}
private Callbacks mCallbacks;
public Adapter() {
}
@Override
public MyRecycleViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_details, null);
return new MyRecycleViewHolder(v);
}
@Override
public void onBindViewHolder(final MyRecycleViewHolder holder, final int i) {
holder.ExpandButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (mCallbacks != null) {
mCallbacks.onButtonClicked(holder.ProbTitle.getText().toString());
}
}
});
}
@Override
public int getItemCount() {
return;
}
public void setCallbacks(Callbacks callbacks) {
this.mCallbacks = callbacks;
}
}
或强>
您可以尝试在onItemClick()
上执行此操作Intent i = new Intent(view.getContext(), DetailsActivity.class);
i.putExtra("title", yourTitle);
i.putExtra("description", yourDescription);
view.getContext().startActivity(i);
当您在DetailActivity中创建oncreate时,请执行此操作
String title = getIntent().getStringExtra("title");
String description = getIntent().getStringExtra("description");
因此您可以将标题和说明传递给DetailActivity