개발 창고/Android

[Android] How to apply SimpleAdapter to ListView

로이제로 2024. 3. 4. 08:00
반응형


  • Example Source
  • Result Screen

TOC is not supported in this version (ex.Mobile)

 



 In our previous post, we talked about the most basic ways to apply ArrayAdapter.

 If you have a question about layout.simple_list_item_1 while writing it this time, wouldn't there be layout.simple_list_item_2 as well?

 I wrote it to solve the curiosity.

 

2024.02.27 - [개발 창고/Android] - [Android] How to use ArrayAdapter in ListView

 

[Android] How to use ArrayAdapter in ListView

When developing Android applications, they often use ListView. Whether it's the web or mobile, the bulletin board type is the most popular function that users want. (If you personalize the bulletin board well, it's very useful.) In the case of the web scre

royzero.tistory.com


Example Source

import android.os.Bundle
import android.widget.ListView
import android.widget.SimpleAdapter
import androidx.appcompat.app.AppCompatActivity


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_test)

        // List date to show in list view (for testing)
        val list = ArrayList<HashMap<String, String?>>()
        val item01 = HashMap<String, String?>()
        item01["key01"] = "list01"
        item01["key02"] = "contents01"
        list.add(item01)
        val item02 = HashMap<String, String?>()
        item02["key01"] = "list02"
        item02["key02"] = "contents02"
        list.add(item02)

        // STEP01. Declare the list view of the layout as a list view called mlistview
        val mListView = findViewById<ListView>(R.id.mListView)

        // STEP02. Settings From
        val from = arrayOf("key01", "key02")

        // STEP03. Settings To
        val to = intArrayOf(android.R.id.text1, android.R.id.text2)

        // STEP04. Call the default Array Adapter to apply to the List.
        val mAdapter = SimpleAdapter(this, list, android.R.layout.simple_list_item_2, from, to)

        // STEP05. Apply the Adapter to the list view.
        mListView.adapter = mAdapter
    }
}

Result Screen

ListView with Simple Adapter


[Android] How to apply SimpleAdapter to ListView

🇰🇷 Korean

2020.07.25 - [개발 창고/Android] - [Android] ListView에 SimpleAdapter 적용하기

 

[Android] ListView에 SimpleAdapter 적용하기

이전 게시글에서는 ArrayAdapter를 적용하는 가장 기본적인 방법에 대해 이야기했습니다. 이번에는 작성하다 보면 한 번쯤 드는 궁금증 layout.simple_list_item_1이 있으면, layout.simple_list_item_2도 있지 않

royzero.tistory.com

 

반응형