
activity_main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" android:orientation="vertical"> <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/list"></ListView> </LinearLayout> |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
package com.androindian.raj.custom import android.support.v7.app.AppCompatActivity import android.os.Bundle import kotlinx.android.synthetic.main.activity_main.* class MainActivity : AppCompatActivity() { var Names= arrayOf("ssss","dddd", "ffff","gggg") var Numbers= arrayOf("111","222","333","3333","999") var Images= arrayOf(R.mipmap.ic_launcher, R.mipmap.ic_launcher,R.mipmap.ic_launcher, R.mipmap.ic_launcher) override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) list.adapter = CustomAdapter( this@MainActivity, Names, Numbers, Images) } } |
CustomAdapter.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package com.androindian.raj.custom import android.content.Context import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.BaseAdapter import kotlinx.android.synthetic.main.listshow.view.* public class CustomAdapter ( mainActivity: MainActivity, internal var Contactnames: Array<String>, internal var Contactnumbers: Array<String>, internal var Contactimages: Array<Int> ) : BaseAdapter(){ private var context: Context? = null override fun getCount(): Int { return Contactimages.size } override fun getItemId(position: Int): Long { return position.toLong() } override fun getItem(position: Int): Any { return position } override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { val rowview = LayoutInflater.from(parent?.getContext()).inflate(R.layout.listshow, parent, false) rowview.tv.text = Contactnames[position] rowview.tv1.text = Contactnumbers[position] rowview.iv.setImageResource(Contactimages[position]) return rowview } } |
listshow.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:id="@+id/ll"> <ImageView android:layout_width="100dp" android:layout_height="100dp" android:id="@+id/iv"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv" android:textColor="@color/colorPrimary" android:textSize="20sp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/tv1" android:textColor="@color/colorAccent" android:textSize="20sp"/> </LinearLayout> </LinearLayout> |
Output: