0

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)

2 Answers2

1

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.

Sergio
  • 27,326
  • 8
  • 128
  • 149
  • Is the lack of camel case the issue or does it solely exist for readability? – 120 099 101 105 110 103 Dec 05 '22 at 14:42
  • 1
    Well, I assumed that it can be the case, did it help? Also the camel case is better for readability, and, according to the Kotlin style guide, Camel case should be used for naming entities. – Sergio Dec 05 '22 at 14:58
0

Firstly, lists are immutable data types(read-mode only), if you want to add/remove elements you should use mutableList.

Secondly, you should declare the list inside the adapter and use a method to update it.

class RecycledViewAdapter(): RecyclerView.Adapter<recycledViewAdapter.recycledViewHolder>() {
    var itemList = listOf<RecycledVieWithModel>()
...
    fun updateAdapter(newList: List<RecycledVieWithModel>){
      itemList = newList.toList()
      notifyDataSetChanged()
    }
...
}

And you should initialize it as :

 val adapter = RecycledViewAdapter()

Now you can update the content list whenever you want without instantiating the adapter.

adapter.updateAdapter(listOf()) // your elements
George
  • 21
  • 1