如何访问Adapter中的参数

发布于 2025-01-19 10:19:40 字数 1663 浏览 0 评论 0原文

我试图获得我传递给适配器的价值。

我的适配器: 重要的代码行。

class TableViewAdapter(var tripsheetlist: Tripsheetlist, driver: String) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>() 

我的主要活动: 重要的代码行。


recyclerViewTripsheetlist.adapter = TableViewAdapter(tripsheetlist, driver)

private fun fetchJson() {

        println("Attempting to Fetch JSON")

        val url = "https://api.letsbuildthatapp.com/youtube/home_feed"

        val request = Request.Builder().url(url).build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object: Callback {


            override fun onFailure(call: Call, e: IOException) {
                println("Failed to execute request")            }

            override fun onResponse(call: Call, response: Response) {
                val body = response.body?.string()
                println(body)

                val gson = GsonBuilder().create()

                val tripsheetlist = gson.fromJson(body, Tripsheetlist::class.java)


                    weightsum(tvTotalweight, tripsheetlist)
                    totaldelNotes(tvTotaldelv,tripsheetlist)
                var driver : String = Spinner(tripsheetlist)


                runOnUiThread {
                    recyclerViewTripsheetlist.adapter = TableViewAdapter(tripsheetlist, driver)
                }
            }
        })
    }

我在适配器中尝试了这些,但没有运气。


  val Drivers: String =  driver

  val Drivers: String =  tripsheetlist.videos.Drivers

不确定如何通过该值。驱动程序在旋转器中选择自己的自我,选定的项目被作为驱动程序传递,然后将其用于过滤我的回收库的输出。

谢谢您的任何帮助。

I am trying to get a value that I passed into my adapter.

My Adapter:
The line of code that matters.

class TableViewAdapter(var tripsheetlist: Tripsheetlist, driver: String) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>() 

My MainActivity:
The line of code that matters.


recyclerViewTripsheetlist.adapter = TableViewAdapter(tripsheetlist, driver)

private fun fetchJson() {

        println("Attempting to Fetch JSON")

        val url = "https://api.letsbuildthatapp.com/youtube/home_feed"

        val request = Request.Builder().url(url).build()

        val client = OkHttpClient()
        client.newCall(request).enqueue(object: Callback {


            override fun onFailure(call: Call, e: IOException) {
                println("Failed to execute request")            }

            override fun onResponse(call: Call, response: Response) {
                val body = response.body?.string()
                println(body)

                val gson = GsonBuilder().create()

                val tripsheetlist = gson.fromJson(body, Tripsheetlist::class.java)


                    weightsum(tvTotalweight, tripsheetlist)
                    totaldelNotes(tvTotaldelv,tripsheetlist)
                var driver : String = Spinner(tripsheetlist)


                runOnUiThread {
                    recyclerViewTripsheetlist.adapter = TableViewAdapter(tripsheetlist, driver)
                }
            }
        })
    }

I have tried these in my adapter but with no luck.


  val Drivers: String =  driver

  val Drivers: String =  tripsheetlist.videos.Drivers

Not sure how to get the value passed. The drivers select them self in the spinner the selected item is passed through as driver and then this is used to filter the output of my recyclerview.

Thank you for any help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

耳钉梦 2025-01-26 10:19:40

简单的回答。我只需要声明我的参数。

class TableViewAdapter(var tripsheetlist: Tripsheetlist,val driver: String) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>()

我认为问题在于我试图在类中声明它,但从未看到对它的引用。然后当我查看时,我看到 tripsheet 被声明为 var 。所以我尝试了一下,到目前为止没有任何问题。

Simple answer. I simply needed to declare my parameter.

class TableViewAdapter(var tripsheetlist: Tripsheetlist,val driver: String) : RecyclerView.Adapter<TableViewAdapter.RowViewHolder>()

I think what the problem was is that I tried to declare it inside my class and never saw a reference to it. Then when I looked I saw that tripsheet was declared as var. So I tried it and no problems thus far.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文