Android Application

Kotlin 어댑터 뷰(Adaptor View) : 리스트뷰(ListView)

작성자 임베디드코리아 작성일23-09-25 01:49 조회1,961회 댓글0건

첨부파일

------------------------------------------------------------------------------------
      activity_main.xml
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <ListView
        android:id="@+id/listView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    layout/list_item_user.xml
------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">

    <ImageView
        android:id="@+id/iv_profile"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="이름"
        app:layout_constraintStart_toEndOf="@+id/iv_profile"
        app:layout_constraintTop_toTopOf="@+id/iv_profile" />

    <TextView
        android:id="@+id/tv_greet"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="안녕하세요"
        app:layout_constraintBottom_toBottomOf="@+id/iv_profile"
        app:layout_constraintStart_toEndOf="@+id/iv_profile"
        app:layout_constraintTop_toBottomOf="@+id/tv_name" />

    <TextView
        android:id="@+id/tv_age"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:text="22"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/tv_name" />

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    //바인딩 객체 선언
    private var mBinding: ActivityMainBinding ?= null
    private val binding get() = mBinding!!

    //리스트 요소 순서에 맞게 선언
    var UserList = arrayListOf<User>(
        User(R.drawable.photo1, "이순신", "34", "010-2222-3333"),
        User(R.drawable.photo2, "김유신", "30", "010-3232-3232"),
        User(R.drawable.photo3, "박정희", "24", "010-2695-1114"),
        User(R.drawable.photo4, "우두리", "22", "010-2121-2121"),
        User(R.drawable.photo5, "강하리", "36", "010-7878-7788"),
        User(R.drawable.photo6, "주세중", "27", "010-5656-5656"),
        User(R.drawable.photo7, "김문수", "25", "010-5656-5566")
    )

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        //setContentView(R.layout.activity_main)

        //바인딩 초기화
        mBinding = ActivityMainBinding.inflate(layoutInflater)

        // 생성된 뷰 액티비티에 표시
        setContentView(binding.root)

        val Adapter = UserAdapter(this, UserList)
        binding.listView.adapter = Adapter


        //클릭한 리스트 이름 토스트메시지로 나타내기
        binding.listView.onItemClickListener = AdapterView.OnItemClickListener { parent, view, position, id ->
            val selectItem = parent.getItemAtPosition(position) as User
            Toast.makeText(this, selectItem.name, Toast.LENGTH_SHORT).show()
        }

    }

    override fun onDestroy() {
        mBinding = null
        super.onDestroy()
    }
}

------------------------------------------------------------------------------------
    User.kt
------------------------------------------------------------------------------------
// 클래스 모델 객체
class User (val profile: Int, val name: String, val age: String, val greet:String)

------------------------------------------------------------------------------------
    UserAdapter.kt
------------------------------------------------------------------------------------
class UserAdapter (val context: Context, val UserList: ArrayList<User>): BaseAdapter() {

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val view : View = LayoutInflater.from(context).inflate(R.layout.list_item_user, null)

        //id로 부터 view를 찾아라
        val profile = view.findViewById<ImageView>(R.id.iv_profile)
        val name = view.findViewById<TextView>(R.id.tv_name)
        val age = view.findViewById<TextView>(R.id.tv_age)
        val greet = view.findViewById<TextView>(R.id.tv_greet)

        val user = UserList[position]

        profile.setImageResource(user.profile)
        name.text= user.name
        age.text = user.age
        greet.text = user.greet

        return view
    }

    override fun getCount(): Int {
        return UserList.size

    }

    override fun getItem(position: Int): Any {
        return UserList[position]
    }

    override fun getItemId(position: Int): Long {
        return 0
    }
}

------------------------------------------------------------------------------------
  build.gradle(Module) : build.gradle.kts
-------------------------------------------------------------------------------------
// 안드로이드 스튜디오 4.0 이상
android {
    ...
    buildFeatures {
        viewBinding = true
    }
}