0

I have a LoadingListView which contains this:

 var adapter: ArrayAdapter<*>? = null
    private set

When I delete a item, I do this:

adapter?.remove(wallboardToDelete)

This says: required Nothing! found MyObject

How can I fix this? I tried using in my ListView ArrayAdapter<Any>? and the removeWorks, but then my init from all my classes that extend gives me an issue saying that: I need ArrayAdapter instead of MyArrayAdapterFromListExtendingThis.

6
  • Try var adapter: ArrayAdapter<Any>? = null. Btw consider using val over var! Commented Aug 22, 2019 at 13:46
  • I want just "wallboardToDelete" to delete, not the full ArrayAdapter. That item is being selected from a "for loop" that checks what to remove Commented Aug 22, 2019 at 13:47
  • That should work with my solution as well. Don't you have a common interface or super class which all items extend from`? Commented Aug 22, 2019 at 13:49
  • From your last section I assume that you use this code in an abstract class or so? Commented Aug 22, 2019 at 13:52
  • Yes. ArrayAdapter is from a class (LoadingListView) that is being Extended by others. pastebin.com/qEAHdt15 Commented Aug 22, 2019 at 13:57

2 Answers 2

2

Try to change your declaration from ArrayAdapter<*> to ArrayAdapter<Any>.
<*> is used for represent a fixed type wich you don't know what is, so it can contain Int type, but only Int then

Sign up to request clarification or add additional context in comments.

3 Comments

Also take a look on this link for more information: kotlinlang.org/docs/reference/generics.html#star-projections
Thanks. I did that and then when I init I would cast to ArrayAdapter<Any> my adapters, and it works now :D
actually very interesting, did not know that <*> is used just for one single type
0

From the code you postes on the website in the comment why don't you use something like this?

class LoadingListView<T> : RelativeLayout {
    var adapter: ArrayAdapter<T>? = null
        private set

    // ...

    fun init(adapter: ArrayAdapter<T>, loadingListListener: LoadingListListener, noTextRes: Int) {
        this.adapter = adapter;
        // ...
    }

    // ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.