Search code examples
androidkotlinandroid-recyclerviewviewmodelrealmrecyclerviewadapter

RecyclerViewAdapter with ArrayList runtime error


I've been getting into android studio lately and I am having a problem with the RecyclerViewAdapter. I have tried using List which does work, but does not allow me to remove or add items in that list. Is there a way to solve this or should I use a ViewModel for that instead?

My relevant code:

class recycledViewAdapter(private var itemmodellist: ArrayList<recycledviewitemmodel>): RecyclerView.Adapter<recycledViewAdapter.recycledViewHolder>() {

val recycledview = binding.recycledview
val list: ArrayList<recycledviewitemmodel> = ArrayList()
for  (i in 1..10){
    list.add(recycledviewitemmodel("item " + i.toString()))
}
val adapter  = recycledViewAdapter(list)
recycledview.adapter = adapter
recycledview.layoutManager = LinearLayoutManager(this)

Error:

Process: com.example.recycleview, PID: 6955
    java.lang.NoSuchMethodError: No direct method <init>(Ljava/util/List;)V in class Lcom/example/recycleview/recycledViewAdapter; or its super classes (declaration of 'com.example.recycleview.recycledViewAdapter' appears in /data/data/com.example.recycleview/code_cache/.overlay/base.apk/classes4.dex)
        at com.example.recycleview.MainActivity.onCreate(MainActivity.kt:25)
        at android.app.Activity.performCreate(Activity.java:8290)
        at android.app.Activity.performCreate(Activity.java:8269)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1384)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3657)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3813)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2308)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loopOnce(Looper.java:201)
        at android.os.Looper.loop(Looper.java:288)
        at android.app.ActivityThread.main(ActivityThread.java:7898)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)


Solution

  • Please name all classes starting with a capital letter, and using camel case, for example:

    class RecycledViewAdapter(
      private var itemModelList: ArrayList<RecycledViewItemModel>
    ) : RecyclerView.Adapter<RecycledViewAdapter.RecycledViewHolder>() { ... }
    

    After renaming the classes clean and rebuild the project.