我正在将对象列表传递给适配器。这些对象属于Uploads
类类型,仅包含Docname
和Docurl
。在每个RecyclerView项目上显示Docname后,我希望当用户单击任何项目时,浏览器应使用与该项目的URL对应的URL打开。
这是我尝试过的。它没有引发任何错误,并且应用程序运行平稳,但是当我单击任何项目时,什么都没有发生。
public class EbookRepresentAdapter extends RecyclerView.Adapter<EbookRepresentAdapter.EbookRepresentViewHolder> {
private Context mContext;
private List<Uploads> mbooks;
public String bookurl;
public EbookRepresentAdapter(Context context, List<Uploads> books) {
mContext = context;
mbooks = books;
}
public class EbookRepresentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public Context usecontext;
public TextView mbookname;
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
}
@Override
public void onClick(View v) {
Intent intent=new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(bookurl));
mContext.startActivity(intent);
}
}
@NonNull
@Override
public EbookRepresentViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v= LayoutInflater.from(mContext).inflate(R.layout.subjectcard,parent,false);
return new EbookRepresentViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull EbookRepresentViewHolder holder, int position) {
Uploads yearSubjectcurrent= mbooks.get(position);
String subname=yearSubjectcurrent.getDocname();
bookurl= yearSubjectcurrent.getDocurl();
//Log.d(TAG,"subname = "+ subname);
holder.mbookname.setText(subname);
}
@Override
public int getItemCount() {
return mbooks.size();
}
}
如果有人可以帮助,那就太好了。
答案 0 :(得分:1)
您需要添加itemView.setOnClickListener(this)
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this)
}
加更改此
public void onBindViewHolder(@NonNull EbookRepresentViewHolder holder, int position) {
Uploads yearSubjectcurrent= mbooks.get(position);
String subname=yearSubjectcurrent.getDocname();
bookurl= yearSubjectcurrent.getDocurl();
//Log.d(TAG,"subname = "+ subname);
holder.mbookname.setText(subname);
到
public void onBindViewHolder(@NonNull EbookRepresentViewHolder holder, int position) {
Uploads yearSubjectcurrent= mbooks.get(position);
String subname=yearSubjectcurrent.getDocname();
//bookurl= yearSubjectcurrent.getDocurl(); //delete this line
//Log.d(TAG,"subname = "+ subname);
holder.mbookname.setText(subname);
}
}
您将覆盖bookurl变量。
并使用
public void onClick(View v) {
int i = getLayoutPosition();
Uploads yearSubjectcurrent= mbooks.get(i);
bookurl= yearSubjectcurrent.getDocurl();
//than Intent Code
}
答案 1 :(得分:0)
您忘记添加itemView.setOnClickListener(this)
只需使用此
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this)
}
答案 2 :(得分:0)
在项目视图上设置onClickListener
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this);
}