首页 > 解决方案 > RecyclerView 的空白屏幕未连接适配器

问题描述

我想在 recyclerview 中解析 JSON。应用程序编译正常,但它输出空/空白屏幕

博客适配器.kt

class BlogAdapter(private val blogList: List<Blog>) : RecyclerView.Adapter<BlogAdapter.ViewHolder>() {


    override fun getItemCount()= blogList.size

    private var mContext: Context? = null

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

        this.mContext=parent.context;

        return ViewHolder(
            LayoutInflater.from(parent.context).inflate(
                R.layout.character_item,
                parent,
                false
            )
        )
    }

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

        val mBlog = this.blogList[position]

        if (mBlog.img != null) {
            Glide.with(mContext!!)
                .load(mBlog.img)
                .into(holder.ivThumbnail)
        }
        if (mBlog.name != null) {
            holder.tvTitle.text = mBlog.name
            println("Log: Kabe "+mBlog.name)
        }
    }

    class ViewHolder(itemView:View):RecyclerView.ViewHolder(itemView){
        val ivThumbnail:ImageView = itemView.findViewById(R.id.ivThumbnail);
        val tvTitle:TextView = itemView.findViewById(R.id.tvTitle);
    }
}

MainActivity.kt

class MainActivity : AppCompatActivity() {

    var mainViewModel: MainViewModel? = null
    var mBlogAdapter: BlogAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        mainViewModel = ViewModelProvider(this).get(MainViewModel::class.java)
        getPopularBlog()
        swipe_refresh.setOnRefreshListener { getPopularBlog() }
    }

    private fun getPopularBlog() {
        swipe_refresh.isRefreshing = false
        mainViewModel!!.allBlog.observe(this, Observer {  charactersList ->
            prepareRecyclerView(charactersList)
        })

    }

    private fun prepareRecyclerView(blogList: List<Blog>) {

        mBlogAdapter = BlogAdapter(blogList)
        if (this.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
            blogRecyclerView.layoutManager = LinearLayoutManager(this)
        } else {
            blogRecyclerView.layoutManager = GridLayoutManager(this, 4)
        }
        blogRecyclerView.itemAnimator = DefaultItemAnimator()
        blogRecyclerView.adapter = mBlogAdapter

    }
}

我的 Json 文件如下所示:

[
  {
    "id": 1,
    "name": "potter",
    "img": "https://images.example.com/potter.jpg"
},
{ …}
]

我根据本教程创建了它:https ://itnext.io/kotlin-wrapping-your-head-around-livedata-mutablelivedata-coroutine-networking-and-viewmodel-b552c3a74eec

请有任何建议

编辑:

class BlogRepository() {

    private var character = mutableListOf<ABCCharacters>()
    private var mutableLiveData = MutableLiveData<List<ABCCharacters>>()
    val completableJob = Job()
    private val coroutineScope = CoroutineScope(Dispatchers.IO + completableJob)

    private val thisApiCorService by lazy {
        RestApiService.createCorService()
    }

    fun getMutableLiveData():MutableLiveData<List<ABCCharacters>> {
        coroutineScope.launch {
            val request = thisApiCorService.getPopularBlog()
            withContext(Dispatchers.Main) {
                try {

                    val response = request.await()
                    val mBlogWrapper = response;
                    if (/*mBlogWrapper != null &&*/ mBlogWrapper.isNotEmpty()) {
                        character = mBlogWrapper as MutableList<ABCCharacters>
                        mutableLiveData.value = character
                    }

                } catch (e: HttpException) {
                    // Log exception //

                } catch (e: Throwable) {
                    // Log error //)
                }
            }
        }
        return mutableLiveData;
    }
}

标签: androidkotlinandroid-recyclerviewretrofit2kotlin-coroutines

解决方案


notifyDataSetChanged当你设置你的小部件时,你忘记打电话了RecyclerView。在完整的方法调用下方,使其工作。

private fun prepareRecyclerView(blogList: List<Blog>) {

    mBlogAdapter = BlogAdapter(blogList)
    if (this.resources.configuration.orientation == Configuration.ORIENTATION_PORTRAIT) {
        blogRecyclerView.layoutManager = LinearLayoutManager(this)
    } else {
        blogRecyclerView.layoutManager = GridLayoutManager(this, 4)
    }
    blogRecyclerView.itemAnimator = DefaultItemAnimator()
    blogRecyclerView.adapter = mBlogAdapter
    mBlogAdapter.notifyDataSetChanged()

}

推荐阅读