Java 如何获取我单击以将其发送到下一个活动的cardview的信息
我正在制作一个应用程序,如果我使用recyclerview显示cardview列表,我希望能够制作用户单击的cardview的详细活动,但我不知道如何将单击的项目发送到下一个活动 在这里您可以看到我的适配器: /** *阿尔贝托于2016年5月18日创作。 */Java 如何获取我单击以将其发送到下一个活动的cardview的信息,java,android,android-studio,android-cardview,Java,Android,Android Studio,Android Cardview,我正在制作一个应用程序,如果我使用recyclerview显示cardview列表,我希望能够制作用户单击的cardview的详细活动,但我不知道如何将单击的项目发送到下一个活动 在这里您可以看到我的适配器: /** *阿尔贝托于2016年5月18日创作。 */ 公共类EmpresasAdapter扩展了RecyclerView.Adapter 实现ItemClickListener{ 私人最终语境; 私人清单项目; 公共EMPRESADAPTER(上下文、列表项){ this.context=
公共类EmpresasAdapter扩展了RecyclerView.Adapter
实现ItemClickListener{
私人最终语境;
私人清单项目;
公共EMPRESADAPTER(上下文、列表项){
this.context=上下文;
这个项目=项目;
}
@凌驾
public int getItemCount(){
返回items.size();
}
@凌驾
公共EMPRESSVIEWHOLDER onCreateViewHolder(视图组视图组,int i){
视图v=LayoutInflater.from(viewGroup.getContext())
.充气(R.layout.empresa_卡、视图组、假);
返回新的EmpresasViewHolder(v,this);
}
@凌驾
BindViewHolder上的公共无效(EmpresasViewHolder,int i){
//项目过程实际发生额
Empresas currentItem=items.get(i);
viewHolder.nombrep.setText(currentItem.getNombre());
viewHolder.Descriptionp.setText(currentItem.GetDescriptionc());
viewHolder.franquiciap.setText(currentItem.getModalidad()+“|”+currentItem.getFranquicia());
//卡加图像
带(上下文)滑动
.load(currentItem.getImagen())
.into(viewHolder.imagenp);
}
@凌驾
公共虚线单击(视图,int位置){
//在运输中心进行图像比较
View sharedImage=View.findViewById(R.id.imagenp);
发射(
(活动)上下文、位置、共享图像);
}
/**
*视图支架para reciclar elementos
*/
公共静态类EmpresasViewHolder扩展了RecyclerView.ViewHolder
实现View.OnClickListener{
//对库索的看法
公共图像查看图像NP;
公共文本视图nombrep;
公共文本视图描述;
公共文本视图franquiciap;
//科姆尼科翁国际酒店
公共项ClickListener侦听器;
公共EmpresasViewHolder(视图v,项ClickListener侦听器){
超级(五);
nombrep=(TextView)v.findViewById(R.id.nombrep);
descripionp=(TextView)v.findViewById(R.id.descripionp);
franquiciap=(TextView)v.findviewbyd(R.id.franquiciap);
imagenp=(ImageView)v.findViewById(R.id.imagenp);
v、 setOnClickListener(此);
this.listener=listener;
}
@凌驾
公共void onClick(视图v){
onItemClick(v,getAdapterPosition());
}
}
将这些项目作为启动DetailedActivity的额外目的:
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
Intent intent = new Intent(context, DetailedActivty.class)
intent.putExtra(nombrep, NOMBREP_EXTRA);
intent.putExtra(descripcionp, DESCRIPCIONP_EXTRA);
intent.putExtra(franquiciap, FRANQUICIAP_EXTRA);
context.startActivity(intent);
}
将这些项目作为启动DetailedActivity的额外目的:
@Override
public void onItemClick(View view, int position) {
// Imagen a compartir entre transiciones
View sharedImage = view.findViewById(R.id.imagenp);
Intent intent = new Intent(context, DetailedActivty.class)
intent.putExtra(nombrep, NOMBREP_EXTRA);
intent.putExtra(descripcionp, DESCRIPCIONP_EXTRA);
intent.putExtra(franquiciap, FRANQUICIAP_EXTRA);
context.startActivity(intent);
}
像这样更改onCreateViewHolder
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empresa_card, viewGroup, false);
v.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//you have the cardView in v;
//you can access the child view of your cardView by referring to v
//if you want to get the position you can do it here.
//Create an intent with the collected data an d start the activity
}
});
return new EmpresasViewHolder(v, this);
}
您也可以在onBindView()
方法中执行此操作。使用此方法可以轻松访问位置。但这并不意味着您不能在前一个方法中使用位置。您可以。但我没有描述它。如果您愿意,可以这样做
//to do this its easier if you have assigned and id to the cardView in XML and store the cardView in the viewHOlder
//getting cardView
CardView cardView=holder.cardView;
cardView.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//position is easily availble now.
//you can access the rest of the views
//create youtr intent and start the activity
}
});
像这样更改onCreateViewHolder
public EmpresasViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.empresa_card, viewGroup, false);
v.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//you have the cardView in v;
//you can access the child view of your cardView by referring to v
//if you want to get the position you can do it here.
//Create an intent with the collected data an d start the activity
}
});
return new EmpresasViewHolder(v, this);
}
您也可以在onBindView()
方法中执行此操作。使用此方法可以轻松访问位置。但这并不意味着您不能在前一个方法中使用位置。您可以。但我没有描述它。如果您愿意,可以这样做
//to do this its easier if you have assigned and id to the cardView in XML and store the cardView in the viewHOlder
//getting cardView
CardView cardView=holder.cardView;
cardView.setOnClickListener(new View.OnClickListener{
@Override
public void onClick(View v){
//position is easily availble now.
//you can access the rest of the views
//create youtr intent and start the activity
}
});
有一种方法可以得到我点击的卡片的数组位置?这样我就可以把它放在额外的位置,我问这个问题是因为我想在细节活动中显示比卡片更多的信息。要在第二个活动中获得信息,我必须使用getExtra()对吗?是的,没错。在第二个活动中,你应该通过getExtra()获得信息。我的问题是,当我使用getnombreextra并在文本视图中打印它时,它会显示com.example.babus.extra.NOMBREUpload您用于设置和获取额外数据的代码。我使用Bundle而不是get extra获取项目(Bundle extras=getIntent().getExtras();)有一种方法可以得到我点击的卡片的数组位置?这样我就可以把它放在额外的位置,我问这个问题是因为我想在细节活动中显示比卡片更多的信息。要在第二个活动中获得信息,我必须使用getExtra()对吗?是的,没错。在第二个活动中,你应该通过getExtra()获得信息。我的问题是,当我使用getnombreextra并在文本视图中打印它时,它会显示com.example.babus.extra.NOMBREUpload您用于设置和获取额外数据的代码。我使用Bundle而不是get extra获取项目(Bundle extras=getIntent().getExtras();)下一个活动中要发送哪个项目?我想获得cardview数组中的位置,以便获得我想要显示的所有信息(与我在cardview中显示的信息相同+其他信息)并在我的详细信息视图中使用它下一个活动中要发送哪个项目?我想获得cardview数组中的位置,这样我就可以获得我想要显示的所有信息(与我在cardview中显示的信息相同+其他信息),并在我的详细信息视图中使用它