从 ViewHolder 获取 ID
Get ID from ViewHolder
我需要点击一张图片并发送一个 URL(意向),但我有很多图片,所以我试图获取 ID,但我找不到合适的方法来做这件事,如果你们可以帮助我,我尝试了我所知道的一切
public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder>
{
Context context;
String[] programTituloList;
String[] programDescList;
int[] programImages;
public static class ViewHolder extends RecyclerView.ViewHolder
{
TextView rowTitulo;
TextView rowDesc;
ImageView rowImage;
public ViewHolder(@NonNull View itemView)
{
super(itemView);
rowTitulo = itemView.findViewById(R.id.txt_titulo);
rowDesc = itemView.findViewById(R.id.txt_desc);
rowImage = itemView.findViewById(R.id.imagem_box);
}
}
public ProgramAdapter(Context context, String[] programTituloList,
String[] programDescList, int[] images) {
this.context = context;
this.programTituloList = programTituloList;
this.programDescList = programDescList;
this.programImages = images;
}
@NonNull
@Override
public ProgramAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.box, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public int getItemCount() {
return programTituloList.length;
}
@Override
public void onBindViewHolder(@NonNull ProgramAdapter.ViewHolder holder, int position) {
holder.rowTitulo.setText(programTituloList[position]);
holder.rowDesc.setText(programDescList[position]);
holder.rowImage.setImageResource(programImages[position]);
holder.rowImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if ()
{
Uri uri = Uri.parse("http://www.google.com");
Intent i = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(i);
}
}
});
}
}
反正 select 图片名称并做一个 Intet url ??
这里有图更好理解
image
https://www.youtube.com/watch?v=SvTr9QA5NvA
我只是用这段视频改编了这段代码并成功了
holder.rowImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Url[holder.getBindingAdapterPosition()]));
context.startActivity(intent);
}
});
我需要点击一张图片并发送一个 URL(意向),但我有很多图片,所以我试图获取 ID,但我找不到合适的方法来做这件事,如果你们可以帮助我,我尝试了我所知道的一切
public class ProgramAdapter extends RecyclerView.Adapter<ProgramAdapter.ViewHolder>
{
Context context;
String[] programTituloList;
String[] programDescList;
int[] programImages;
public static class ViewHolder extends RecyclerView.ViewHolder
{
TextView rowTitulo;
TextView rowDesc;
ImageView rowImage;
public ViewHolder(@NonNull View itemView)
{
super(itemView);
rowTitulo = itemView.findViewById(R.id.txt_titulo);
rowDesc = itemView.findViewById(R.id.txt_desc);
rowImage = itemView.findViewById(R.id.imagem_box);
}
}
public ProgramAdapter(Context context, String[] programTituloList,
String[] programDescList, int[] images) {
this.context = context;
this.programTituloList = programTituloList;
this.programDescList = programDescList;
this.programImages = images;
}
@NonNull
@Override
public ProgramAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater inflater = LayoutInflater.from(context);
View view = inflater.inflate(R.layout.box, parent, false);
ViewHolder viewHolder = new ViewHolder(view);
return viewHolder;
}
@Override
public int getItemCount() {
return programTituloList.length;
}
@Override
public void onBindViewHolder(@NonNull ProgramAdapter.ViewHolder holder, int position) {
holder.rowTitulo.setText(programTituloList[position]);
holder.rowDesc.setText(programDescList[position]);
holder.rowImage.setImageResource(programImages[position]);
holder.rowImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if ()
{
Uri uri = Uri.parse("http://www.google.com");
Intent i = new Intent(Intent.ACTION_VIEW, uri);
context.startActivity(i);
}
}
});
}
} 反正 select 图片名称并做一个 Intet url ??
这里有图更好理解 image
https://www.youtube.com/watch?v=SvTr9QA5NvA
我只是用这段视频改编了这段代码并成功了
holder.rowImage.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(Url[holder.getBindingAdapterPosition()]));
context.startActivity(intent);
}
});