Difference between revisions of "Kotlin Adapter"
From zen2
(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...") |
|||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
| + | =Basic= | ||
| + | This will create a lot of reads, so only good for small data sets | ||
| + | |||
*Create a List View to display your items | *Create a List View to display your items | ||
*In a separate layout file create an item view | *In a separate layout file create an item view | ||
| Line 20: | Line 23: | ||
categoryView.categoryImage.setImageResource(resourceID) | categoryView.categoryImage.setImageResource(resourceID) | ||
categoryView.categoryText.text = categories[position].title | 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() | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | Then in MainActivity or where you are calling this from: | ||
| + | <pre> | ||
| + | class MainActivity : AppCompatActivity() { | ||
| + | |||
| + | lateinit var adapter:CategoryAdapter | ||
| + | |||
| + | override fun onCreate(savedInstanceState: Bundle?) { | ||
| + | super.onCreate(savedInstanceState) | ||
| + | setContentView(R.layout.activity_main) | ||
| + | adapter = CategoryAdapter(this,DataService.categories) | ||
| + | categoryListView.adapter = adapter | ||
| + | } | ||
| + | } | ||
| + | </pre> | ||
| + | |||
| + | =Using a Holder= | ||
| + | A holder will hold the view for reusing | ||
| + | <pre> | ||
| + | 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 | ||
| + | val holder: ViewHolder | ||
| + | |||
| + | if (convertView == null) { //The first time the views are presented | ||
| + | categoryView = LayoutInflater.from(context).inflate(R.layout.category_list_item,null) | ||
| + | holder = ViewHolder() | ||
| + | holder.categoryImage = categoryView.findViewById(R.id.categoryImage) | ||
| + | holder.categoryTitle = categoryView.findViewById(R.id.categoryText) | ||
| + | println("I exist for the first time") | ||
| + | categoryView.tag = holder | ||
| + | } else { | ||
| + | holder = convertView.tag as ViewHolder | ||
| + | categoryView = convertView | ||
| + | println("Recycled") | ||
| + | } | ||
| + | |||
| + | val category = categories[position] | ||
| + | |||
| + | val resourceID = context.resources.getIdentifier(categories[position].image,"drawable",context.packageName) | ||
| + | holder.categoryImage?.setImageResource(resourceID) | ||
| + | holder.categoryTitle?.text = category.title | ||
return categoryView | return categoryView | ||
} | } | ||
| Line 35: | Line 100: | ||
} | } | ||
| + | private class ViewHolder { | ||
| + | var categoryImage: ImageView? = null | ||
| + | var categoryTitle: TextView? = null | ||
| + | } | ||
} | } | ||
</pre> | </pre> | ||
Latest revision as of 22:41, 29 November 2017
Basic
This will create a lot of reads, so only good for small data sets
- 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()
}
}
Then in MainActivity or where you are calling this from:
class MainActivity : AppCompatActivity() {
lateinit var adapter:CategoryAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
adapter = CategoryAdapter(this,DataService.categories)
categoryListView.adapter = adapter
}
}
Using a Holder
A holder will hold the view for reusing
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
val holder: ViewHolder
if (convertView == null) { //The first time the views are presented
categoryView = LayoutInflater.from(context).inflate(R.layout.category_list_item,null)
holder = ViewHolder()
holder.categoryImage = categoryView.findViewById(R.id.categoryImage)
holder.categoryTitle = categoryView.findViewById(R.id.categoryText)
println("I exist for the first time")
categoryView.tag = holder
} else {
holder = convertView.tag as ViewHolder
categoryView = convertView
println("Recycled")
}
val category = categories[position]
val resourceID = context.resources.getIdentifier(categories[position].image,"drawable",context.packageName)
holder.categoryImage?.setImageResource(resourceID)
holder.categoryTitle?.text = category.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()
}
private class ViewHolder {
var categoryImage: ImageView? = null
var categoryTitle: TextView? = null
}
}
