RecyclerView Android错误:E / RecyclerView:未连接适配器;跳过布局

时间:2020-04-17 20:52:28

标签: android kotlin android-recyclerview

尝试从API获取JSON数据并将其显示在RecyclerView上时,出现以下错误:

Error: No adapter attached; skipping layout

我有时已经使用了RecyclerView,但从未遇到过这个问题。

MainActivity:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(activity_main)
    overwriteOnPostInteractionListener()
    setupObservers()
}

private fun setupObservers(){
    mServiceRequest.searchPostsFromAPI().observe(this, Observer { posts ->
        if (posts != null){
            loadRecyclerView()
            mPostList = posts.toMutableList()
        }
    })
}

private fun loadRecyclerView() {
    recyclerView.adapter = PostListAdapter(mPostList, mOnPostListInteractionListener)
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.setHasFixedSize(true)
}

适配器:

class PostListAdapter(private val postList: List<PostModel>,
      private val onPostListInteractionListener: OnPostListInteractionListener):
      RecyclerView.Adapter<PostViewHolder>(){ 

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PostViewHolder {
    val inflate = LayoutInflater.from(parent.context)
    val view = inflate.inflate(R.layout.posts , parent, false)
    return PostViewHolder(view, parent.context, onPostListInteractionListener)
}

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

override fun onBindViewHolder(holder: PostViewHolder, position: Int) {
    holder.bindTask(postList[position])
}}

ViewHolder:

class PostViewHolder(itemView: View, private val context: Context,
                 private val onPostListInteractionListener: OnPostListInteractionListener)
                : RecyclerView.ViewHolder(itemView) {

private val postTitle = itemView.findViewById<TextView>(R.id.titleTextViewMain)
private val postBody = itemView.findViewById<EditText>(R.id.bodyEditText)

fun bindTask(post: PostModel){
    postTitle.text = post.title
    postBody.setText(post.body)
    postTitle.setOnClickListener {
        onPostListInteractionListener.onListClick(post.id)
    }
}}

我搜索了很多方法来解决此错误,但我做不到。

1 个答案:

答案 0 :(得分:2)

您在将数据输入列表之前先构建RecyclerView适配器,因此需要在实例化适配器之前调用mPostList = posts.toMutableList()

因此,将setupObservers()更改为:

private fun setupObservers(){
    mServiceRequest.searchPostsFromAPI().observe(this, Observer { posts ->
        if (posts != null){
            mPostList = posts.toMutableList()
            loadRecyclerView()

        }
    })
}

还要在完全构建RecyckerView之后设置适配器。因此,如下更改loadRecyclerView()的顺序。

private fun loadRecyclerView() {
    recyclerView.layoutManager = LinearLayoutManager(this)
    recyclerView.setHasFixedSize(true)
    recyclerView.adapter = PostListAdapter(mPostList, mOnPostListInteractionListener)
}

在布局前设置RecyclerView适配器可能会导致问题。

旁注:

每次更改列表时都实例化适配器,这不是正确的方法,最好在生命周期中仅创建一次您的东西(包括适配器),然后在每次要更改它们时创建setter而不是一遍又一遍地实例化它们。

因此,您只能在onCreate()方法中实例化适配器一次,并在适配器中创建一个方法来接受帖子setPosts(private val postList: List<PostModel>)并在内部进行设置..每当您需要更改适配器列表时,致电setPosts