SimpleRecyclerView icon indicating copy to clipboard operation
SimpleRecyclerView copied to clipboard

Unable to get it working with Kotlin

Open ka05 opened this issue 7 years ago • 3 comments

Im using kotlin and have been following the guide in the readme.

Problem 1 I came across the section below in the readme and i cannot get it to work.

class BookCell(item: Book) : SimpleCell<Book>(item) {
  override fun getLayoutRes(): Int {
    return R.layout.cell_book
  }

  override fun onBindViewHolder(holder: SimpleViewHolder, position: Int, context: Context, payload: Any) {
    holder.textView.text = item.title
  }
}

Android Studio does not recognize textView in this line: holder.textView.text = item.title

I did create the layout file and have a textView with the matching Id so im not sure whats going on.

I was under the impression that maybe you were doing some sort of compile time generation to generate the holder to be used based off of the layout file, but that cant be because com.jaychang.srv.kae.SimpleCell uses SimpleViewHolder explicitly which only extends com.jaychang.srv.SimpleViewHolder.

Problem 2 Cant create list of SimpleCell type in kotlin that is shown in the Multiple Types section of the readme.

List<Book> books = DataUtils.getBooks();
List<Ad> ads = DataUtils.getAds();
List<SimpleCell> cells = new ArrayList<>();

My version, In Kotlin

val books:MutableList<Book> = DataUtils.getBooks();
val ads:MutableList<Ad> = DataUtils.getAds();
val cells:MutableList<SimpleCell> = mutableListOf()

Android Studio is complaining about SimpleCell in the list declaration ( MutableList<SimpleCell> ) stating that I must add the Type argument.

One type argument expected for class SimpleCell<T>

I am very confused and would greatly appreciate it if you could provide a more detailed guide for using this library with kotlin and perhaps several kotlin example activities in the source.

I suppose for the time being i can write this in java but that also means other areas of my app that are already in kotlin will have to be converted back to java, which is a bummer. :(

ka05 avatar Jun 08 '18 14:06 ka05

Problem 1: Have you added kotlin android extension plugin?

Problem 2: SimpleCell<T> is a parameterized type, so you should write val cells:MutableList<SimpleCel<*>> = mutableListOf().

jaychang0917 avatar Jun 08 '18 14:06 jaychang0917

@jaychang0917, Yes i am using the kotlin android extension plugin. I even tried cleaning and building to make sure but no dice.

Correct me if Im wrong but doesn't that plugin only work when referencing views directly? like below:

textView.text = item.title

The example show this though which I haven't seen work like this anywhere else:

holder.textView.text = item.title

Perhaps I need to just remove holder? Wouldn't that cause the holder pattern to break though?

ka05 avatar Jun 08 '18 16:06 ka05

Make sure you added

androidExtensions {
    experimental = true
}

jaychang0917 avatar Jun 09 '18 03:06 jaychang0917