Hello everyone, I have some problem about Cast `ja...
# android
s
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
are you sure you’re passing proper
data
to the function?
s
I have updated my question again for clarify
w
Again, I’m pretty sure you’re passing wrong
data
object
image.png
s
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
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
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
And how do you set data in
data
?
I mean the adapter’s list
r
That kind of cast cant work. You cant convert a object to a arraylist.
w
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
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
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
thanks @wasyl, I figured out this problem…Many thanks.
w
@Sam so what was the issue?
s
This is my stupid mistake to checking wrong type in adapter :(((( Thanks @wasyl @rkeazor