单击 RecyclerView 中的特定项目后如何打开 URL?
How to open URL after clicking at a particular item in RecyclerView?
我正在将一个对象列表传递给我的适配器。这些对象的类型为 Uploads
class,其中仅包含 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();
}
}
如果有人能提供帮助就太好了。
您忘记添加itemView.setOnClickListener(this)
就用这个,
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this)
}
您需要添加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
}
在您的项目视图上设置 onClickListener
作为
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this);
}
我正在将一个对象列表传递给我的适配器。这些对象的类型为 Uploads
class,其中仅包含 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();
}
}
如果有人能提供帮助就太好了。
您忘记添加itemView.setOnClickListener(this)
就用这个,
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this)
}
您需要添加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
}
在您的项目视图上设置 onClickListener 作为
public EbookRepresentViewHolder(View itemView) {
super(itemView);
mbookname = itemView.findViewById(R.id.subjectname);
usecontext=itemView.getContext();
itemView.setOnClickListener(this);
}