gpt4 book ai didi

android - RecyclerView 获取点击的item详情 - Android

转载 作者:行者123 更新时间:2023-11-29 15:07:13 26 4
gpt4 key购买 nike

我是安卓新手。我正在尝试使用 this 创建一个 RecyclerView教程。 RecyclerView 工作正常。我的问题是,我想获取 RecyclerView 的点击项目的标题和描述。

有人可以帮助我吗? Here是我的完整代码。

已更新

    public class View_Holder extends RecyclerView.ViewHolder implements View.OnClickListener{

CardView cv;
TextView title;
TextView description;
ImageView imageView;


View_Holder(View itemView) {
super(itemView);
cv = (CardView) itemView.findViewById(R.id.cardView);
title = (TextView) itemView.findViewById(R.id.title);
description = (TextView) itemView.findViewById(R.id.description);
imageView = (ImageView) itemView.findViewById(R.id.imageView);

//added by me
title.setOnClickListener(this);
description.setOnClickListener(this);
imageView.setOnClickListener(this);
}

//added by me

public ImageView getImageView() {
return imageView;
}
public TextView getTitle() {
return title;
}
public TextView getDescription() {
return description;
}

@Override
public void onClick(View v) {
// Toast.makeText(get, "CLICK", Toast.LENGTH_SHORT).show();
Log.i("positon-of-clicked-item", String.valueOf(getAdapterPosition()));
Log.i("positon-of-clicked-item", String.valueOf(getLayoutPosition()));
Log.i("positon-of-clicked-item", String.valueOf(this.getTitle()));
Log.i("positon-of-clicked-item", String.valueOf(this.getDescription()));
// getLayoutPosition(); //no method like that
}

最佳答案

只需使用 ViewHolder View 对象即可获取如下所示的详细信息。

@Override
public void onClick(View v) {
// Toast.makeText(get, "CLICK", Toast.LENGTH_SHORT).show();
Log.i("positon-of-clicked-item", String.valueOf(getAdapterPosition()));
Log.i("positon-of-clicked-item", String.valueOf(getLayoutPosition()));
Log.i("positon-of-clicked-item", String.valueOf(title.getText());
Log.i("positon-of-clicked-item", String.valueOf(sescription.getText()));
// getLayoutPosition(); //no method like that
}

关于android - RecyclerView 获取点击的item详情 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35823495/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com