Skip to content

Instantly share code, notes, and snippets.

@raveesh-me
Last active October 21, 2017 12:41
Show Gist options
  • Save raveesh-me/50593f665a614c82365aea905ad6c200 to your computer and use it in GitHub Desktop.
Save raveesh-me/50593f665a614c82365aea905ad6c200 to your computer and use it in GitHub Desktop.
An performance optimized ListView adapter without viewHolder implementation.
package com.example.raveesh.stormy.adapters
import android.content.Context
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import com.example.raveesh.stormy.R
import com.example.raveesh.stormy.weather.Day
import kotlinx.android.synthetic.main.daily_list_item.view.*
import org.jetbrains.anko.imageResource
import org.jetbrains.anko.layoutInflater
internal class DayAdapter(mCtx: Context,
private val mDays: Array<Day>) : BaseAdapter() {
private val mInflater = mCtx.layoutInflater
private lateinit var row : View
override fun getItem(position: Int): Day = mDays[position]
override fun getCount(): Int = mDays.size
override fun getItemId(position: Int): Long = 0//not used
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View{
row = convertView ?: mInflater.inflate(R.layout.daily_list_item, parent, false)
/*make sure to initialize each and every view in the order you want drawn.
use an if-else block and put the non reetetive assignments into the if null block but need
to initialise at least once:->
*/
row.dailyCircleImageView.imageResource = R.drawable.bg_temperature
row.dailyTemperatureLabel.text = "${Math.round(mDays[position].mTemperatureMax)}"
row.dailyIconImageView.imageResource = mDays[position].getIconId()
row.dailyNameLabel.text = mDays[position].getDayOfTheWeek()
return row
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment