Actions

Kotlin Adapter

From zen2

Revision as of 20:37, 29 November 2017 by Chris (talk | contribs) (Created page with "*Create a List View to display your items *In a separate layout file create an item view *Create a new file with Kotlin Class, call it XXXAdapter or whatever. eg <pre> class C...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  • Create a List View to display your items
  • In a separate layout file create an item view
  • Create a new file with Kotlin Class, call it XXXAdapter or whatever. eg
class CategoryAdapter(context:Context, categories:List<Category>): BaseAdapter() {}

You will have to override getView, getItem, getItemId, getCount

class CategoryAdapter(context:Context, categories:List<Category>): BaseAdapter() {

    val context = context
    val categories = categories

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val categoryView :View

        categoryView = LayoutInflater.from(context).inflate(R.layout.category_list_item,null)
      
        val resourceID = context.resources.getIdentifier(categories[position].image,"drawable",context.packageName)
        categoryView.categoryImage.setImageResource(resourceID)
        categoryView.categoryText.text = categories[position].title
        return categoryView
    }

    override fun getItem(position: Int): Any {
        return categories[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }

    override fun getCount(): Int {
        return categories.count()
    }

}