【问题标题】:Why does my RecyclerView shows the same value every iteration?为什么我的 RecyclerView 每次迭代都显示相同的值?
【发布时间】:2019-02-25 09:17:03
【问题描述】:

它显示了正确数量的对象,但为它们提供了相同的值。

背景: 我正在学习构建一个类似 Slack 的应用程序。我正在使用回收站视图来显示频道列表。

这是我的适配器

class ChannelsAdapter(val context: Context, val channels: ArrayList<Channel>) :
    RecyclerView.Adapter<ChannelsAdapter.Holder>() {

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val singleChannel = itemView.findViewById<TextView>(R.id.single_channel)

        fun bindText(textVar: String, context: Context) {
            singleChannel.text = textVar
        }
    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bindText(channels[position].toString(), context)
    }

    override fun getItemCount(): Int {
        return channels.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {

        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.channel_list_layout, parent, false)
        return Holder(view)

    }
}

这是“频道”类:

class Channel(val name: String, val Description: String, val id: String) {

    override fun toString(): String {
        return ("#$name")
    }

}

这是分配给 RecyclerView 的数据变量,以及从服务器拉取数据的函数和

object MessageService {

    val channels = ArrayList<Channel>()

    fun getChannels(context: Context, complete: (Boolean) -> Unit) {

        val channelsRequest =
            object : JsonArrayRequest(Method.GET, URL_GET_CHANNELS, null, Response.Listener { response ->

                try {

                    for (x in 0 until (response.length())) {
                        val channel = response.getJSONObject(0)
                        val channelName = channel.getString("name")
                        val channelDescription = channel.getString("description")
                        val channelId = channel.getString("_id")

                        val newChannel = Channel(channelName, channelDescription, channelId)
                        this.channels.add(newChannel)

                    }
                    complete(true)

                } catch (e: JSONException) {
                    Log.d("JSON", "EXC:" + e.localizedMessage)
                    complete(false)
                }

            }, Response.ErrorListener {
                Log.d("ERROR", "Could not retrieve channels")
                complete(false)
            }) {

                override fun getBodyContentType(): String {
                    return "application/json; charset=utf-8"
                }

                override fun getHeaders(): MutableMap<String, String> {
                    val headers = HashMap<String, String>()

                    headers.put("Authorization", "Bearer ${AuthService.authToken}")
                    return headers
                }
            }

        Volley.newRequestQueue(context).add(channelsRequest)

    }

}

这个 val 以 JsonArray 的形式从服务器获取数据。

【问题讨论】:

  • 可以尝试在getItemCount()中使用size()而不是count()
  • @Maxouille count() 工作正常,是数据显示不正确
  • 尝试在bindText()方法中打印textVar的值,看它是否改变。无论如何,您应该查看有关如何在 kotlin 中实现自定义 recyclerview 的 android 文档:developer.android.com/guide/topics/ui/layout/… 尝试将您的 recyclerview 写为示例,看看它是否效果更好。
  • 我已经实现了几次自定义 recyclerView 适配器,并且一直在将其与我所做的其他适配器进行比较,但我真的找不到我做错了什么。
  • 我可以建议您在 bondText() 方法中打印您的 arrayList 以及 textVar 以查看发生了什么。您可以在此处发布日志

标签: android json kotlin android-recyclerview


【解决方案1】:

只需像这样更改您的onBindViewHolder

override fun onBindViewHolder(holder: Holder, position: Int) {
        holder.bindText(channels[position], context)
    }

而你的bindText 函数是这样的

inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val singleChannel = itemView.findViewById<TextView>(R.id.single_channel)

        fun bindText(data: Channel, context: Context) {
            singleChannel.text = data.name
        }
    }

【讨论】:

  • 很遗憾没有帮助
  • @Tsabary 现在你面临什么问题?
【解决方案2】:

检查您在哪里初始化 Chanel 并传入频道列表。确保每个通道都在将通道附加到列表的循环内初始化。不久前发生在我身上,例如

   while(true) {
   Channel myChanel= new Channel()
   myChanel.name = "name"
   channels.add(myChanel)
   } 
// Pass list into adapter. Convert to kotlin :)

【讨论】:

  • 感谢您的回答。实际上我根本没有使用循环,所以不确定这是否有帮助。
  • 抱歉,Philip 我的错误,正在使用一个循环,并且已经在其中初始化。
  • 尝试在将`newChannel.name`添加到频道数组列表后立即记录它。只是为了确保循环按预期工作。抱歉回复晚了。
【解决方案3】:

我的问题在于从服务器拉取信息的循环,愚蠢的错误。

for (x in 0 until (response.length())) {
                        val channel = response.getJSONObject(0)
                        val channelName = channel.getString("name")
                        val channelDescription = channel.getString("description")
                        val channelId = channel.getString("_id")

我一直在拉对象“0”而不是“x”

【讨论】:

    猜你喜欢
    • 2019-03-17
    • 1970-01-01
    • 2012-10-03
    • 2023-03-22
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多