Hi guys i want to make listview with dynamic load data in arraylist using model(getter/setter). How can i do.?
Could you please explain in more detail which part of the task presents difficulties for you? Are you building an Android application?
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.
What do you mean by “I don’t get any resource”? You can use any Java libraries in Kotlin, including Volley.
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…?
Hello,
Download source code from (Listview in Kotlin Android).
Model.kt:
public class Model_flower{
var str_name:String =“”
var str_des:String =“”
var int_image:Int=0constructor(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.TextViewclass 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: ImageViewinit { 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!
Thanks !