gpt4 book ai didi

android - 从 RecyclerView 项目传递参数

转载 作者:行者123 更新时间:2023-11-29 01:37:10 25 4
gpt4 key购买 nike

我有以下 RecyclerView 适配器。显示这些项目工作正常,但是我想在单击其中一个项目时将参数传递给对话框或 fragment :

 public class NovaAdapter extends RecyclerView.Adapter<NovaListRowHolder> {

ArrayList<HashMap<String, String>> novaList = new ArrayList<HashMap<String, String>>();
public static final String STATUS = "status";
public static final String NAME = "name";
public static final String ID = "id";
private Context mContext;

public NovaAdapter(Context context, ArrayList<HashMap<String, String>> novaList) {
this.novaList = novaList;
this.mContext = context;
}

@Override
public NovaListRowHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.instances_list, null);
NovaListRowHolder mh = new NovaListRowHolder(v);
return mh;
}

@Override
public void onBindViewHolder(NovaListRowHolder novaListRowHolder, int i) {

HashMap<String, String> e = novaList.get(i);
novaListRowHolder.name.setText(e.get(NAME));
novaListRowHolder.status.setText(e.get(STATUS));
novaListRowHolder.setId(e.get(ID));


}


@Override
public int getItemCount() {
return (null != novaList ? novaList.size() : 0);
}class NovaListRowHolder extends RecyclerView.ViewHolder implements View.OnClickListener{
protected TextView name;
protected TextView status;
protected String id;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public NovaListRowHolder(View view) {
super(view);
view.setOnClickListener(this);
this.name = (TextView) view.findViewById(R.id.nameInstance);
this.status = (TextView) view.findViewById(R.id.statusInstance);

}

public void onClick(View view){
Dialog dialog = new Dialog(view.getContext());
dialog.setContentView(R.layout.instances_listdetail);
dialog.setTitle("Details " + name.getText() + " " + getPosition());
dialog.show();
}

目前点击只是打开一个占位符对话框,但它没有传递任何数据。我试图到处搜索有关如何执行此操作的示例或教程,但在任何地方都找不到。

提前抱歉,它太明显了,但这是我的第一个应用程序,我没有太多经验。

最佳答案

您应该为每个 View 设置 onBindViewHolder 中的 onClickListener。您可以直接从 viewHolder 访问 itemView。现在您可以使用点击监听器为 View 设置一个(或几个)标签,并在您的 onClick 方法中检索它们。

关于android - 从 RecyclerView 项目传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27221308/

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