它显示了正确数量的对象,但为它们提供了相同的值。
背景: 我正在跟着做一个构建类似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从服务器获取其数据。
答案 0 :(得分:1)
只需更改您的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
}
}
函数是这样
alias
答案 1 :(得分:0)
检查初始化Chanel并在通道列表中传递的位置。确保在将Channel追加到列表的循环内初始化每个Channel。不久前发生在我身上
while(true) {
Channel myChanel= new Channel()
myChanel.name = "name"
channels.add(myChanel)
}
// Pass list into adapter. Convert to kotlin :)
答案 2 :(得分:0)
我的问题是在循环中从服务器提取信息,这是愚蠢的错误。
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”