我有一个回收站视图,可以根据我的列表动态生成视图中的元素。但是我不知道如何动态地将我的URL分配给网站图像,这样当用户点击我的UI中的网站图像时,它将打开一个新的浏览器,并为其分配URL。
例如,如果用户点击第一张卡片R.drawable.website图片,它将打开"我的网址1"。如果他点击第二张卡R.drawable.website图片,它将打开"我的网址2"。
我想在我的主要活动中使用intent,但我看到的所有示例都指定了网站URL,具体意味着我只能指定1个网址。我想知道这是否可以在 RVAdapter类中完成。
RVAdapter.java
import android.content.Intent;
import android.net.Uri;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.ImageView;
import com.example.utsav.edufind.R;
import java.util.List;
import entity.PolytechnicCourse;
import entity.UniversityCourse;
import entity.Course;
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.CourseViewHolder> {
public static class CourseViewHolder extends RecyclerView.ViewHolder {
CardView cv;
TextView CourseName;
TextView SchoolName;
TextView CourseGrade;
TextView CourseIntake;
ImageView CourseWebsite;
ImageView SchoolLogo;
TextView gradeTitle;
CourseViewHolder(View itemView) {
super(itemView);
cv = itemView.findViewById(R.id.cv);
CourseName = itemView.findViewById(R.id.Course_name);
CourseGrade= itemView.findViewById(R.id.Course_Grade);
SchoolName = itemView.findViewById(R.id.School_name);
CourseIntake = itemView.findViewById(R.id.Course_Intake);
CourseWebsite = itemView.findViewById(R.id.Course_Website);
SchoolLogo = itemView.findViewById(R.id.School_Logo);
gradeTitle = itemView.findViewById(R.id.Course_Grade_Title);
}
}
List<Course> courses;
public RVAdapter(List<Course> courses){
this.courses = courses;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public CourseViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
CourseViewHolder pvh = new CourseViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(CourseViewHolder CourseViewHolder, int i) {
if(courses.get(i) instanceof PolytechnicCourse) {
switch (courses.get(i).getSchool()) {
case "Singapore Polytechnic": {
CourseViewHolder.SchoolLogo.setImageResource(R.mipmap.sp);
break;
}
case "Ngee Ann Polytechnic": {
CourseViewHolder.SchoolLogo.setImageResource(R.mipmap.np);
break;
}
case "Republic Polytechnic": {
CourseViewHolder.SchoolLogo.setImageResource(R.mipmap.rp);
break;
}
case "Nanyang Polytechnic": {
CourseViewHolder.SchoolLogo.setImageResource(R.mipmap.nyp);
break;
}
case "Temasek Polytechnic": {
CourseViewHolder.SchoolLogo.setImageResource(R.mipmap.tp);
break;
}
default:
}
CourseViewHolder.SchoolName.setText(courses.get(i).getSchool());
CourseViewHolder.CourseName.setText(courses.get(i).getCourseName());
CourseViewHolder.CourseGrade.setText(String.valueOf(((PolytechnicCourse) courses.get(i)).getL1R4()));
CourseViewHolder.CourseIntake.setText(String.valueOf(courses.get(i).getIntake()));
CourseViewHolder.CourseWebsite.setImageResource(R.drawable.website);
// I want to retrieve the url from String.valueOf(courses.get(i).getWebsite());
// and put them into a clickable image url (R.drawable.website)
// In my main UI, when i click on the website logo, it will open up the url in a browser
CourseViewHolder.CourseWebsite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Problem 1 (variable i is not accessible within inner class)
//Problem 2 (startActivity cannot be resolved)
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(String.valueOf(courses.get(i).getWebsite())));
startActivity(browserIntent);
}
});
CourseViewHolder.gradeTitle.setText("L1R4");
}
这是我在主页的列表中放入的内容
SearchResultsUI.java
private void initializeData(){
courseList = new ArrayList<>();
courseList.add(new PolytechnicCourse("Diploma in Business Management", "Nanyang Polytechnic",25, 13, "my web URL1"));
courseList.add(new PolytechnicCourse("Diploma in Digital Media", "Singapore Polytechnic", 400, 11, "my web URL2"));
courseList.add(new PolytechnicCourse("Diploma in Accountancy", "Singapore Polytechnic", 6700, 19, "my web URL3"));
courseList.add(new PolytechnicCourse("Diploma in Biological Sciences", "Temasek Polytechnic", 300, 12, "my web URL4"));
}
这是我的XML文件
items.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/cv"
android:layout_marginBottom="20dp"
android:elevation="3dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="0dp"
android:background="@drawable/relative_background_with_shadow"
>
<ImageView
android:id="@+id/School_Logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:contentDescription="School Logo"/>
<TextView
android:id="@+id/Course_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/School_Logo"
android:layout_marginTop="15dp"
android:textSize="16sp"
android:textColor="@color/colorAccent"/>
<TextView
android:id="@+id/School_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/Course_name"
android:layout_toEndOf="@+id/School_Logo"
android:textSize="13sp"
android:textColor="@color/gray"/>
<View
android:id="@+id/view"
style="@style/Divider"
android:layout_below="@+id/School_Logo"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/view"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:background="@drawable/linear_background_with_shadow"
android:gravity="center">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:layout_marginEnd="40dp">
<ImageView
android:id="@+id/Course_Grade_Circle"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="3dp"
android:background="@drawable/greencircle"/>
<TextView
android:id="@+id/Course_Grade"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:width="80dp"
android:textSize="22sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/white"
android:paddingStart="9dp"
android:paddingEnd="20dp"/>
<TextView
android:id="@+id/Course_Grade_Title"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Course_Grade_Circle"
android:textSize="16sp"
android:layout_marginTop="7dp"
android:text="@string/L1R4"
android:textAlignment="center"
android:layout_marginStart="4dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center">
<ImageView
android:id="@+id/Course_Intake_Circle"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:background="@drawable/greencircle"/>
<TextView
android:id="@+id/Course_Intake"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:minWidth="80dp"
android:maxWidth="200dp"
android:layout_marginStart="0dp"
android:textSize="22sp"
android:textAlignment="center"
android:textStyle="bold"
android:textColor="@color/white"/>
<TextView
android:id="@+id/Course_Intake_Title"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Course_Intake_Circle"
android:textSize="16sp"
android:layout_marginTop="7dp"
android:text="@string/intake"
android:textAlignment="center"
android:layout_marginStart="8dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginStart="40dp">
<ImageView
android:id="@+id/Course_Website"
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_marginTop="10dp"
android:contentDescription="Course Website Icon"/>
<TextView
android:id="@+id/Course_Website_Title"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_below="@+id/Course_Website"
android:textSize="16sp"
android:text="@string/website"
android:layout_marginTop="7dp"
android:textAlignment="center"
android:layout_marginStart="6dp"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
答案 0 :(得分:2)
CourseViewHolder.CourseWebsite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("<add url here>"));
mContext.startActivity(browserIntent);
}
});
从对象中获取url并添加它。
如果您不知道如何传递上下文,请按照以下步骤操作
在创建适配器对象时将活动上下文作为第二个参数
的活动RVAdapter rvAdapter = new RVAdapter(CourseList,this);
在循环视图类中获取上下文
Context mContext;
public RVAdapter(List<Course> courses,Context context){
this.courses = courses;
mContext = context
}
答案 1 :(得分:1)
首先,您需要将setOnClickListener函数添加到ImageView。
CourseViewHolder.CourseWebsite.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
接下来,您需要修改RVAdapter构造函数,使其具有context属性。 Context将用于startActivity。
List<Course> courses;
// Add this property
Context context
// Modify constructor
public RVAdapter(Context context, List<Course> courses){
this.context = context
this.courses = courses;
}
接下来,在setOnClickListener
中@Override
public void onClick(View v) {
final String website = this.courses.get(i).getWebsite()
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(website));
this.context.startActivity(browserIntent);
}