# How to use getter setter in kotlin for listview

**URL:** <https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041>\
**Category:** Android\
**Created:** [June 1, 2017, 9:40am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041 "2017-06-01T09:40:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![codevitaboy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/codevitaboy/32/3703_2.png) [@codevitaboy](https://discuss.kotlinlang.org/u/codevitaboy)\
**Post date:** [June 1, 2017, 9:40am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/1 "2017-06-01T09:40:25Z")

</div>

Hi guys i want to make listview with dynamic load data in arraylist using model(getter/setter). How can i do.?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 2, 2017, 11:21am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/2 "2017-06-02T11:21:12Z")

</div>

Could you please explain in more detail which part of the task presents difficulties for you? Are you building an Android application?

---

<div class="post-metadata">

**Author:** ![codevitaboy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/codevitaboy/32/3703_2.png) [@codevitaboy](https://discuss.kotlinlang.org/u/codevitaboy)\
**Post date:** [June 3, 2017, 6:02am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/3 "2017-06-03T06:02:58Z")

</div>

ya… i’m building android application but i’m trying to bind json data in model with arraylist but nither i get any resource for the get json data like java…(Like http Asynchtask OR Volly for networking call to get json data…) nor getting bind json data in model.

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [June 3, 2017, 8:20am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/4 "2017-06-03T08:20:43Z")

</div>

What do you mean by “I don’t get any resource”? You can use any Java libraries in Kotlin, including Volley.

---

<div class="post-metadata">

**Author:** ![codevitaboy](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/codevitaboy/32/3703_2.png) [@codevitaboy](https://discuss.kotlinlang.org/u/codevitaboy)\
**Post date:** [June 6, 2017, 9:04am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/5 "2017-06-06T09:04:51Z")

</div>

this is my adapter.kt

class testAdapter( val context: Context, var rowItems: ArrayList) : BaseAdapter() {

```
override fun getCount(): Int {
    return rowItems.size
}

override fun getItem(i: Int): ArtGallery {
    return rowItems[i]
}

override fun getItemId(i: Int): Long {
    return i.toLong()
}

constructor(rowItems: ArrayList<ArtGallery>) : this(context , rowItems) {
    this.rowItems = rowItems;
}

override fun getView(i: Int, convertView: View?, parent: ViewGroup): View {
    var convertView = convertView
    if (convertView == null) {
       convertView= LayoutInflater.from(parent.getContext()).inflate(R.layout.listview_row, parent, false);
    }

```

}

and here where i’m set adapter to listview…like this…  
{  
val restaurant\_list: ListView = findViewById(R.id.restaurant\_lv) as ListView

```
    rowItems = ArrayList()
    val list_adapter = testAdapter(applicationContext,rowItems)
    restaurant_list.adapter = list_adapter

```

}

using volley

{  
val repoName = jsonObject.getString(“Restaturant\_name”)  
val fullName = jsonObject.getString(“Category”)  
val img\_url = jsonObject.getString(“img”)

```
                    val items = ArtGallery(repoName, fullName, img_url)
                    items.artist = repoName
                    items.title = fullName
                    items.main_image = img_url

                    rowItems.add(items)

```

}  
but i didn’t get data i adapter nor in arraylist…where i’m doing mistake…?

---

<div class="post-metadata">

**Author:** ![deepshikha\_puri](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/deepshikha_puri/32/3912_2.png) [@deepshikha\_puri](https://discuss.kotlinlang.org/u/deepshikha_puri)\
**Post date:** [September 9, 2017, 2:58pm UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/6 "2017-09-09T14:58:05Z")

</div>

Hello,

Download source code from ([Listview in Kotlin Android](http://deepshikhapuri.blogspot.in/2017/09/listview-in-kotlin-android.html)).

**Model.kt:**

> public class Model\_flower{  
> var str\_name:String =“”  
> var str\_des:String =“”  
> var int\_image:Int=0
> 
> ```
> constructor(str_name:String,int_image:Int,str_des:String){
> this.int_image=int_image
> this.str_name=str_name
> this.str_des=str_des
> 
> }
> 
> ```

**CustomAdapter.kt:**

> package com.deepshikha.listviewinkotlin
> 
> import android.content.Context  
> import android.view.LayoutInflater  
> import android.view.View  
> import android.view.ViewGroup  
> import android.widget.BaseAdapter  
> import android.widget.ImageView  
> import android.widget.TextView
> 
> class CustomAdapter(context: Context,al\_flower:ArrayList\<Model\_flower\>) : BaseAdapter(){
> 
> ```
> private val mInflator: LayoutInflater
> private val al_flower:ArrayList<Model_flower>
> 
> init {
> this.mInflator = LayoutInflater.from(context)
> this.al_flower=al_flower
> }
> 
> override fun getCount(): Int {
> return al_flower.size
> }
> 
> override fun getItem(position: Int): Any {
> return al_flower.get(position)
> }
> 
> override fun getItemId(position: Int): Long {
> return position.toLong()
> }
> 
> override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
> val view: View?
> val vh: ListRowHolder
> if (convertView == null) {
> view = this.mInflator.inflate(R.layout.adapter_layout, parent, false)
> vh = ListRowHolder(view)
> view.tag = vh
> } else {
> view = convertView
> vh = view.tag as ListRowHolder
> }
> 
> vh.label.text = al_flower.get(position).str_name
> vh.tv_des.text = al_flower.get(position).str_des
> vh.iv_image.setImageResource(al_flower.get(position).int_image)
> return view
> }
> 
> ```
> 
> }
> 
> private class ListRowHolder(row: View?) {  
> public val label: TextView  
> public val tv\_des: TextView  
> public val iv\_image: ImageView
> 
> ```
> init {
> this.label = row?.findViewById<TextView>(R.id.tv_name) as TextView
> this.tv_des = row?.findViewById<TextView>(R.id.tv_des) as TextView
> this.iv_image = row?.findViewById<ImageView>(R.id.iv_flower) as ImageView
> }
> 
> ```
> 
> }

Thanks!

---

<div class="post-metadata">

**Author:** ![Niko](https://avatars.discourse-cdn.com/v4/letter/n/91b2a8/32.png) [@Niko](https://discuss.kotlinlang.org/u/Niko)\
**Post date:** [April 28, 2018, 4:49am UTC](https://discuss.kotlinlang.org/t/how-to-use-getter-setter-in-kotlin-for-listview/3041/7 "2018-04-28T04:49:48Z")

</div>

Thanks !
