Karu Skipper
Karu Skipper

Reputation: 39

Accessing elements in an object Kotlin

I am trying to access individual elements inside an object, which will then be displayed as part of the view. The goal is to have each element assigned to variables outside the object.

My adapter

private class MyCustomAdapter(list: List<Object>?, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
    private val listings = list;
    private val totalCount = totalCount;

    override fun getItemCount(): Int {
        if (listings != null) {
            return listings.size
        }
        return 0
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context);
        val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
        return CustomViewHolder(cellForRow);
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
            val obj = listings?.get(position);
            holder.view.textView_auction_title?.text = obj.toString()
    }

}

listings?.get(position) looks like this:

     {
        id = 2, 
        title = "testtitle1", 
        category = pants,
     }

However I am trying to display id, title and category as individual elements, rather than displaying the whole object as a text. I am very new to Kotlin so any help would be appreciated.

Upvotes: 2

Views: 767

Answers (2)

Nocturnale
Nocturnale

Reputation: 26

If incoming json data is always on same format you should add em to an arraylist before using and you should have a class model for those variables; (Do this in your fragment or activity, not in adapter.)

var listings = ArrayList<IncomingData>

//class model for data:

class IncomingData(val id: Int, val title: String, val category: String)

//add incoming data to array type of IncomingData:(in fragment or activity)

for (jsonindex in 0 until list.count()) {
    listings.add(
        IncomingData(
             jsonArray.getJSONObject(jsonIndex).getInt("id"),
             jsonArray.getJSONObject(jsonIndex).getString("title"),
             jsonArray.getJSONObject(jsonIndex).getString("category")
        )
    )
             
}

//now you can send that data to your adapter:

recyclerView.adapter = MyCustomAdapter(requireActivity(), listings, this@yourfragment_or_avtivity)

//after that you can access to data from your adapter:

private class MyCustomAdapter(private val listFeed: ArrayList<IncomingData>, totalCount: Int): RecyclerView.Adapter<CustomViewHolder>() {
    
    private val totalCount = totalCount

    override fun getItemCount(): Int {
        return listFeed.count()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomViewHolder {
        val layoutInflater = LayoutInflater.from(parent.context);
        val cellForRow = layoutInflater.inflate(R.layout.auction_main, parent, false);
        return CustomViewHolder(cellForRow);
    }

    override fun onBindViewHolder(holder: CustomViewHolder, position: Int) {
            val obj = listFeed.get(position)
            holder.view.textView_auction_title?.text = obj.title
    }

}

Upvotes: 1

Brad
Brad

Reputation: 300

You shouldn't use List<Object>.

If all of your data list objects follow the same id,title,category structure then you should be making a model out of a class for that List.

If you have varying types of data in that list, then you could either use List<Any> and do something like if (obj is IdTitleCategoryModel) view.text = obj.title or use List<JSONObject> (which I would never recommend over a type specific model)

Upvotes: 3

Related Questions