Advertisement
Carlos_Chaves

List View (Kotlin)

Jan 11th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.79 KB | None | 0 0
  1. package com.example.carloschaves.alugar000
  2.  
  3. import android.content.Context
  4. import android.graphics.Color
  5. import android.support.v7.app.AppCompatActivity
  6. import android.os.Bundle
  7. import android.view.View
  8. import android.view.ViewGroup
  9. import android.widget.BaseAdapter
  10. import android.widget.ListView
  11. import android.widget.TextView
  12.  
  13. class MainActivity : AppCompatActivity() {
  14.  
  15.     override fun onCreate(savedInstanceState: Bundle?) {
  16.         super.onCreate(savedInstanceState)
  17.         setContentView(R.layout.activity_main)
  18.         val listView = findViewById<ListView>(R.id.main_listview)
  19.         val redColor = Color.parseColor( "#FF0000")
  20.         listView.setBackgroundColor(redColor)
  21.  
  22.         listView.adapter = MyCustomAdapter(  this) // this needs to be my custom adapter telling my list what to rendering
  23.     }
  24.  
  25.         private class MyCustomAdapter (context: Context): BaseAdapter() {
  26.  
  27.             private val mContext: Context
  28.  
  29.             init {
  30.                 mContext = context
  31.             }
  32.  
  33.             // responsible for how many rows in may list
  34.             override fun getCount(): Int {
  35.                 return 5
  36.             }
  37.  
  38.             //you can also ignore this
  39.             override fun getItemId(position: Int): Long{
  40.                 return position.toLong()
  41.             }
  42.  
  43.             //you can ignore this for now
  44.             override fun getItem(position: Int): Any {
  45.                 return "Test String"
  46.             }
  47.  
  48.             // responsible for rendering out each row
  49.             override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View {
  50.                 val textView = TextView(mContext)
  51.                 textView.text = "Here is my Row for my listview"
  52.                 return textView
  53.             }
  54.  
  55.         }
  56.  
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement