https://kotlinlang.org logo
#android
Title
# android
s

Sam

09/09/2019, 10:51 AM
Hello everyone, I have some problem about Cast
java.lang.ClassCastException: java.lang.Object cannot be cast to String[]
I want to cast Object to Array of Strings.
Copy code
class BlockToday(val type: TodayType,var data:Any)

enum class TodayType{
    BANNER,EVENTS,CATEGORIES,TOP_SALES
}

val images = arrayListOf<String>()
val block = BlockToday(TodayType.BANNER,images)


=> bindBanner(block.data)
fun bindBanner(data:Any){
        val images = data as ArrayList<String> => Crash 
}
Any ideas to resolve this problem. ??? Thanks
w

wasyl

09/09/2019, 10:53 AM
are you sure you’re passing proper
data
to the function?
s

Sam

09/09/2019, 10:55 AM
I have updated my question again for clarify
w

wasyl

09/09/2019, 11:00 AM
Again, I’m pretty sure you’re passing wrong
data
object
image.png
s

Sam

09/09/2019, 11:09 AM
I pass object to view holder
data
have something in there
Screen Shot 2019-09-09 at 6.08.31 PM.png,Screen Shot 2019-09-09 at 6.08.43 PM.png
Weird
w

wasyl

09/09/2019, 11:10 AM
Isn’t the problem already there in
getItem(position).data
? What’s
getItem(position)
class?
Debugger should show you proper types already, not just
Object
.
s

Sam

09/09/2019, 11:12 AM
getItem(position)
=>
Copy code
abstract class BaseRecyclerAdapter<M, V : RecyclerView.ViewHolder> : RecyclerView.Adapter<V>() {

    private var data = ArrayList<M>()
fun getItem(position: Int): M {
        return data[position]
    }
}

`
Copy code
class TodayAdapter(val context: Context?) : BaseRecyclerAdapter<BlockToday, BlockTodayHolder>() {
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BlockTodayHolder {
        val inflate = when (viewType) {
            TodayType.TOP_SALES.ordinal -> R.layout.item_top_sale
            TodayType.CATEGORIES.ordinal -> R.layout.item_categories
            TodayType.EVENTS.ordinal -> R.layout.item_events
            else -> R.layout.item_banner
        }
        return BlockTodayHolder(LayoutInflater.from(context).inflate(inflate, parent, false))
    }

    override fun onBindViewHolder(holder: BlockTodayHolder, position: Int) {
        when(getItem(position).type){
            TodayType.TOP_SALES -> TodayType.TOP_SALES.ordinal
            TodayType.CATEGORIES -> TodayType.CATEGORIES.ordinal
            TodayType.EVENTS -> TodayType.EVENTS.ordinal
            TodayType.BANNER -> {
                holder.bindBanner(getItem(position).data)
            }
        }
    }
here
w

wasyl

09/09/2019, 11:15 AM
And how do you set data in
data
?
I mean the adapter’s list
r

rkeazor

09/09/2019, 11:47 AM
That kind of cast cant work. You cant convert a object to a arraylist.
w

wasyl

09/09/2019, 11:54 AM
Of course it can’t 😉 We’re trying to figure out why the data is an object and not an array list, as should be
r

rkeazor

09/09/2019, 11:54 AM
You are def passing in the wrong stuf... You have a typed adapter of M . Data is a ArrayList<m> but getItem is returning M.... I dont understand why it would work lol
Or maybe I'm tripping lol. Am I looking at this wrongly lol
Why would the compiler infer that it's a Arraylist<String>
Ok M is BlockToday which has a data as type Any ... which means when you set it to a Arraylist , you cast the original object to Any right. And when you try to cast it agian. Your actually Casting Object to Arraylist
Am I wrong?
That's why your seeing it as Object
Because BlockToday signiture says it's a Object. If you explicitly change that to Arraylist<String> it will probably work
w

wasyl

09/09/2019, 12:33 PM
To me it all looks like perfectly valid code
Copy code
import org.junit.Test

class BlockToday(val type: TodayType, var data: Any)

enum class TodayType {
    BANNER, EVENTS, CATEGORIES, TOP_SALES
}

abstract class BaseRecyclerAdapter<M, V> {

    var data = ArrayList<M>()

    fun getItem(position: Int): M = data[position]
}

class BlockTodayHolder {
    fun bindBanner(data: Any) {
        val images = data as ArrayList<String>
        println(images)
    }
}

class TodayAdapter : BaseRecyclerAdapter<BlockToday, BlockTodayHolder>() {

    fun onBindViewHolder(holder: BlockTodayHolder, position: Int) {
        when (getItem(position).type) {
            TodayType.TOP_SALES -> TodayType.TOP_SALES.ordinal
            TodayType.CATEGORIES -> TodayType.CATEGORIES.ordinal
            TodayType.EVENTS -> TodayType.EVENTS.ordinal
            TodayType.BANNER -> {
                holder.bindBanner(getItem(position).data)
            }
        }
    }
}

class Test {

    @Test
    fun name() {
        TodayAdapter().apply {
            data = arrayListOf(
                BlockToday(TodayType.BANNER, arrayListOf("element"))
            )

            onBindViewHolder(BlockTodayHolder(), 0)
        }
    }
}
This compiles and properly prints the list. So @Sam you need to figure out where you set your recycler items and what you actually put there as
data
, because it’s not an array list
s

Sam

09/10/2019, 8:32 AM
thanks @wasyl, I figured out this problem…Many thanks.
w

wasyl

09/10/2019, 8:33 AM
@Sam so what was the issue?
s

Sam

09/10/2019, 8:36 AM
This is my stupid mistake to checking wrong type in adapter :(((( Thanks @wasyl @rkeazor
4 Views