Search code examples
androidkotlinlistviewarraylistadapter

Getting a weird response from an OnItemClickHandler in Android Studio (Kotlin)


I am implementing an OnItemClickHandler on a ListView in Android Studio. For some reason, when I display the selected item, I get a weird string and not the text of the item clicked:

"com.skupad.skupadmobilekotlin.model.PendingStockOutListResponse@e3a35cc"

If I click another item in the list view, the value after the @ will of course change.

The values in index 0 = "TEST100" and in index 1 = "TEST1001".

I don't know how why it's not returning the actual item. Can someone help? Below is the code I'm using to return the item clicked:

                lst_pending_stock_out_list_items.onItemClickListener =
                    OnItemClickListener { p0, p1, p2, p3 ->
                        val item = lst_pending_stock_out_list_items.adapter.getItem(p2).toString()
                        val itemValue = lst_pending_stock_out_list_items.getItemAtPosition(p2).toString()

                        txt_header.text = itemValue
                    }

I also tried:

 val item = lst_pending_stock_out_list_items.adapter.getItem(p2).toString()

with the same result.

EDIT: I want to add how I am loading the data ...

            // storing for later use
                pendingStockOutListArrayList = response.body()!!.result as ArrayList<PendingStockOutListResponse>?

                // initialize adapter
                baseAdapter =
                    PendingStockOutListBaseAdapter(context, R.layout.pending_stock_out_list_adapter_view_layout, pendingStockOutListArrayList)
                lst_pending_stock_out_list_items.adapter = baseAdapter

What I am requesting is being returned properly.


Solution

  • It looks like you have a "PendingStockOutListResponse" model, right?

    When you call

    lst_pending_stock_out_list_items.adapter.getItem(p2).toString()
    

    You are transforming the entire object to string.

    If you wanna show a specific field (Let's consider you have "name" field on "PendingStockOutListResponse".

    You need to call

    lst_pending_stock_out_list_items.adapter.getItem(p2).name.toString()