如何在RecyclyrView的适配器内使浏览器具有意图

时间:2019-05-06 06:27:55

标签: android-intent kotlin android-recyclerview

列表项包含指向网页的链接。当您单击一个项目时,应调用打开链接的浏览器。问题在于,当您尝试调用Intent时,会发生与上下文有关的错误。

帮助纠正错误,如果可能,请编写正确的方法。谢谢

class NewsAdapter(val newsList: NewsList): RecyclerView.Adapter<CustomViewHolderNews>() {

override fun getItemCount(): Int {

    return newsList.results.count()
}

override fun onCreateViewHolder(p0: ViewGroup, p1: Int): CustomViewHolderNews {

    val layoutInflater = LayoutInflater.from(p0.context)
    val cellForRow = layoutInflater.inflate(R.layout.news_row, p0, false)

    return CustomViewHolderNews(cellForRow)
}

override fun onBindViewHolder(holder: CustomViewHolderNews, position: Int) {

    val item = newsList.results.get(position)
    holder.view.title.text = item.title

    val newsImage = holder.view.news_image
    Picasso.get().load(item.imgs.get(0).img).resize(1000 , 600).centerCrop().into(newsImage)

    newsImage.setOnClickListener{
        val intent = Intent(Intent.ACTION_VIEW, Uri.parse(item.site_url))
        startActivity(intent)
    }
}
}

class CustomViewHolderNews(val view: View): RecyclerView.ViewHolder(view){}

1 个答案:

答案 0 :(得分:1)

尝试

用于初始化适配器

private lateinit var adapterNews: NewsAdapter

adapterNews = NewsAdapter(arrayList, mContext)

对于适配器

NewsAdapter(private val arrayList: ArrayList<String>, private val mContext: Activity) : RecyclerView.Adapter<CustomViewHolderNews>() {


newsImage.setOnClickListener{
    val intent = Intent(Intent.ACTION_VIEW, Uri.parse(item.site_url))
    mContext.startActivity(intent)
}