Home > database >  No Adapter Attached RecyclerView
No Adapter Attached RecyclerView

Time:06-02

I want to implement my app. My app went smoothly, but I also got an error with 2022-05-31 14:14:32.663 8626-8626/id.kotlin.belajar E/RecyclerView: No adapter attached; skipping layout; skipping layout.

E/RecyclerView: No adapter attached; skipping layout; skipping layout

Home Activity:

class HomeActivity : DaggerAppCompatActivity(), HomeView {
@Inject
  lateinit var presenter: HomePresenter

  private lateinit var progressBar: ProgressBar
  private lateinit var recyclerView: RecyclerView

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

      progressBar = findViewById(R.id.pb_home)
      recyclerView = findViewById(R.id.rv_home)

      presenter.discoverMovie()
  }

  override fun onShowLoading() {
      progressBar.visibility = View.VISIBLE
  }
  override fun onHideLoading() {
      progressBar.visibility = View.GONE
      recyclerView.visibility = View.VISIBLE
  }

  override fun onResponse(results: List<Result<String>>) {
      recyclerView.addItemDecoration(DividerItemDecoration(this@HomeActivity, 
  DividerItemDecoration.VERTICAL))
      recyclerView.adapter = HomeAdapter(results)
  }

  override fun onFailure(t:Throwable) {
      Log.e(HomeActivity::class.java.simpleName, "${t.printStackTrace()}")
  }
}

Home Adapter:

class HomeAdapter (private val results: List<Result>): 
RecyclerView.Adapter<HomeAdapter.HomeViewHolder>(){

  override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HomeViewHolder {
      return HomeViewHolder(
          LayoutInflater
              .from(parent.context).inflate(
                  R.layout.item_home,
                  parent,
                  false
              )
      )
  }

  override fun onBindViewHolder(holder: HomeViewHolder, position: Int){
      holder.bind()
  }
  override fun getItemCount(): Int{
      return results.count()
  }

  inner class HomeViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
      fun bind() {
          with(itemView) {
              val title = findViewById<TextView>(R.id.original_title)
              title.text

              val overview = findViewById<TextView>(R.id.overview)
              overview.text
          }
      }
  }
}

Home Presenter:

 class HomePresenter(private val view: HomeView, datasource: HomeDatasource) {
  fun discoverMovie(){
  view.onShowLoading()

      val dataSource = Networkmodule.providesHttpAdapter(client = 
  OkHttpClient()).create(HomeDatasource::class.java)
      dataSource.discoverMovie().enqueue(object : Callback<HomeResponse> {
          override fun onResponse(call: Call<HomeResponse>, response: Response<HomeResponse>){
              view.onHideLoading()
              view.onResponse(((response.body()?.results ?: emptyList()) as List<Result<String>>))
      }
          override fun onFailure(call: Call<HomeResponse>, t:Throwable){
              view.onHideLoading()
              view.onFailure(t)
          }
      })
  }
}

I need your help.

CodePudding user response:

I can see two major problems here :

Adapter instantiation

You do not need to re-instantiate the adapter each time you receive data. Just instanciate it in the onCreate method with an empty list and update the list each time you have new data (don't forget to call notify method).

And attach the adapter to the recyclerView in the onCreate method as well

Layout manager missing ?

We don't have your xml code so I don't know if you added the layoutManager on the xml like this (assuming you want a linearManager) :

app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

You can do it programaticaly using the layoutManager attribute on the recyclerView :

recyclerView.layoutManager = LinearLayoutManager()

To avoid errors like that, the best way is to setup your recyclerView as soon as possible in the activity (onCreate) or fragment (onViewCreated) lifecycle.

CodePudding user response:

This is my .xml files:

activity_home:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
   xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   tools:context=".presentation.HomeActivity">

   <androidx.appcompat.widget.Toolbar
       android:layout_width="match_parent"
       android:layout_height="wrap_content"
       android:background="@color/white"
       android:elevation="4dp"
       app:layout_constraintTop_toTopOf="parent"
       app:title="Belajar"
       app:titleTextColor="@color/black"/>

   <ProgressBar
       android:id="@ id/pb_home"
       style="@style/Widget.AppCompat.ProgressBar"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:indeterminate="true"
       android:indeterminateTint="@color/black"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       tools:visibility="gone" />

   <androidx.recyclerview.widget.RecyclerView
       android:id="@ id/rv_home"
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:paddingTop="?android:attr/actionBarSize"
       android:scrollbars="vertical"

       app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent"
       tools:listitem="@layout/item_home" />

</androidx.constraintlayout.widget.ConstraintLayout>
  • Related