首页 > 解决方案 > 列表未显示在 onChangedof 视图模型下

问题描述

我有一个活动,其中详细信息已从服务器获取...我已使用 viewmodel 进行改造...休息显示的详细信息,但内部水平回收视图未显示...加载时崩溃...

这是我的活动代码:---------

 model = ViewModelProvider(this)[ProductViewModel::class.java]
    model.ProductDetails?.observe(this, object : Observer<Product_base_response?> {
        override fun onChanged(t: Product_base_response?) {
            text.setText(t?.data?.name.toString())
            textcost.setText(t?.data?.cost.toString())
            text2.setText(t?.data?.producer.toString())
            descript.setText(t?.data?.description)
            myRatingBar.rating = rating.toFloat()
            Glide.with(getApplicationContext())
                .load(t?.data?.product_images?.get(0)?.image).into(
                    imagemain!!
                )
           //loading list here but crashing
            val retro11: List<Product_images_response>? =
                t?.data?.product_images
            //Log.e("kdjn",t?.data?.product_images!!.toString())
            generateDataList(retro11!!)
        }
        })

    model.loaddetails(id.toString())
    
}
    fun generateDataList(dataList: List<Product_images_response>) {
        val recyclerView=findViewById<RecyclerView>(R.id.Recycleviewdetails)
        val adapter =Custom_detail_Adapter(applicationContext, dataList)
        recyclerView.adapter=adapter
        val linear:LinearLayoutManager=
            LinearLayoutManager(applicationContext, LinearLayoutManager.HORIZONTAL, false)
        recyclerView.layoutManager=linear
        recyclerView.setHasFixedSize(true)
    }

viewmodel:----(不显示完整的代码只是相关的)

   fun loaddetails(product_id:String)
{
    RetrofitClient.instance.fetchUserdetail(product_id)
        .enqueue(object : Callback<Product_base_response> {
            override fun onFailure(call: Call<Product_base_response>, t: Throwable) {
                Log.d("res", "" + t)

            }

            override fun onResponse(
                call: Call<Product_base_response>,
                response: Response<Product_base_response>
            ) {

                _productDetails!!.value = response.body()

            }
        })

适配器:-=------

class  Custom_detail_Adapter(
context: Context,
dataList: List<Product_images_response>) :
RecyclerView.Adapter<Custom_detail_Adapter.CustomViewHolder>() {
private val dataList: List<Product_images_response>
private val context: Context

private var selectedPos: Int = 0

inner class CustomViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
    val mView: View

     val avatar: ImageView
    val relativeitem:RelativeLayout

    init {
        mView = itemView

        avatar = mView.findViewById(R.id.view3)
         relativeitem=mView.findViewById(R.id.relativeitem)

        }
}

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
    val layoutInflater = LayoutInflater.from(parent.context)
    val view: View = layoutInflater.inflate(R.layout.product_detail_item, parent, false)
    return CustomViewHolder(view)
}

override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
  ///  holder.relativeitem.setBackgroundColor(Color.parseColor("#000000"));
    holder.relativeitem.setSelected(selectedPos == position);
 Glide.with(context).load(dataList[position].image)
        .thumbnail(0.5f)
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(holder.avatar)


    holder.avatar.setOnClickListener(View.OnClickListener {
            notifyItemChanged(selectedPos)
            selectedPos = position
            notifyItemChanged(selectedPos)

        val intent = Intent("custom-message")
        intent.putExtra("item", dataList.get(position).image)
        LocalBroadcastManager.getInstance(context).sendBroadcast(intent)
    })
}

override fun getItemCount(): Int {
    return dataList.size }

init {
    this.context = context
    this.dataList = dataList
}
}

但我在 logcat 下得到了一个数组响应

在调试这一行时 --> Log.e("kdjn",t?.data?.product_images.toString())

[Product_images_response(id=39, product_id=13, image=http:/2.jpg, created=2015-09-14T10:18:23+0000, modified=2015-09-14T10:18:23+0000), Product_images_response(id=40, product_id=13, image=7.jpg, created=2015-09-14T10:21:38+0000, modified=2015-09-14T10:21:38+0000), Product_images_response(id=41, product_id=13, image=http://9.jpg, created=2015-09-14T10:22:09+0000, modified=2015-09-14T10:22:09+0000), Product_images_response(id=42, product_id=13, image=http://1.jpg, created=2015-09-14T10:22:59+0000, modified=2015-09-14T10:22:59+0000)]

标签: androidkotlinretrofitretrofit2

解决方案


推荐阅读