์ถ์ฒ: https://sesac.seoul.kr/ (JetPack๊ณผ Kotlin์ ํ์ฉํ Android App ๊ฐ๋ฐ ๊ฐ์)
ํ ํญ๋ชฉ์ ๋ฌธ์์ด ๋ฐ์ดํฐ ํ๋๋ฅผ ๋์ดํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉ
values ํด๋ ๋ฐ์ arrays.xml ํ์ผ ์์ฑ
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="locations">
<item>๊ฐ์๋</item>
<item>๊ฒฝ๊ธฐ๋</item>
<item>๊ฒฝ์๋จ๋</item>
<item>๊ฒฝ์๋ถ๋</item>
<item>๊ด์ฃผ๊ด์ญ์</item>
</string-array>
</resources>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</ListView>
MainActivity.kt
package com.tutorial.c30
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val listView = findViewById<ListView>(R.id.main_list)
val data = resources.getStringArray(R.array.locations)
val adapter = ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
data
)
listView.adapter = adapter
}
}
public ArrayAdapter(@NonNull Context context, int resource, @NonNull T[] objects) {
throw new RuntimeException("Stub!");
}
ํ ํญ๋ชฉ์ ๋ฌธ์์ด ๋ฐ์ดํฐ ์ฌ๋ฌ ๊ฐ๋ฅผ ๋์ดํ๊ณ ์ถ์ ๊ฒฝ์ฐ์ ์ฌ์ฉ
activiti_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</ListView>
MainActivity.kt
package com.tutorial.c32
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_main)
val listView = findViewById<ListView>(R.id.main_list)
val data: ArrayList<HashMap<String, String>> = ArrayList()
var map: HashMap<String, String> = HashMap()
map["name"] = "LG ํธ์์ค"
map["content"] = "์์ธ, ์ ์ค ์ผ๊ตฌ์ฅ"
data.add(map)
map = HashMap()
map["name"] = "๋์ฐ ๋ฒ ์ด์ค"
map["content"] = "์์ธ, ์ ์ค ์ผ๊ตฌ์ฅ"
data.add(map)
map = HashMap()
map["name"] = "KT ์์ฆ"
map["content"] = "์์, KT ์์ฆ ํํฌ"
data.add(map)
val adapter = SimpleAdapter(
this,
data,
android.R.layout.simple_list_item_2,
arrayOf("name", "content"),
intArrayOf(android.R.id.text1, android.R.id.text2)
)
listView.adapter = adapter
}
}
public SimpleAdapter(
Context context,
List<? extends Map<String, ?>> data,
int resource,
String[] from, int[] to) {
throw new RuntimeException("Stub!");
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<ListView
android:id="@+id/main_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:orientation="horizontal">
<EditText
android:id="@+id/edit"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:inputType="text"/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="add"/>
</LinearLayout>
</LinearLayout>
package com.tutorial.c33
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.Button
import android.widget.EditText
import android.widget.ListView
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
var todo = mutableListOf<String>()
lateinit var adapter: ArrayAdapter<String>
lateinit var listView: ListView
lateinit var editText: EditText
lateinit var button: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
listView = findViewById(R.id.main_list)
editText = findViewById(R.id.edit)
button = findViewById(R.id.button)
adapter = ArrayAdapter(
this,
android.R.layout.simple_list_item_1,
todo
)
listView.adapter = adapter
// ๋ฆฌ์คํธ๋ทฐ์ ํญ๋ชฉ์ ํด๋ฆญํ๋ฉด ํญ๋ชฉ ์ ๊ฑฐ ์ฌ๋ถ๋ฅผ ๋ฌป๋ ๋ค์ด์ผ๋ก๊ทธ ๋์ฐ๊ธฐ
listView.setOnItemClickListener { adpaterView, view, i, l ->
AlertDialog.Builder(this)
.setTitle("Remove TODO")
.setPositiveButton("OK") { dialog, which ->
todo.removeAt(i)
adapter.notifyDataSetChanged()
}
.setNegativeButton("CANCEL", null)
.create()
.show()
}
// add ๋ฒํผ ๋๋ฅด๋ฉด ๋ฆฌ์คํธ๋ทฐ์ ํญ๋ชฉ ์ถ๊ฐ
button.setOnClickListener {
todo.add(editText.text.toString())
editText.text.clear()
adapter.notifyDataSetChanged()
}
}
}
custom_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/custom_item_type_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/custom_item_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/custom_item_type_image"
android:layout_marginStart="16dp"/>
<TextView
android:id="@+id/custom_item_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/custom_item_title"
android:layout_alignStart="@id/custom_item_title"/>
<ImageView
android:id="@+id/custom_item_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:src="@drawable/ic_menu"/>
</RelativeLayout>
DriveVO.kt
package com.tutorial.c34
data class DriveVO(
var type: String,
var title: String,
var date: String
)
DriveHolder.kt
package com.tutorial.c34
import android.view.View
import android.widget.ImageView
import android.widget.TextView
class DriveHolder(root: View) {
var typeImageView: ImageView
var titleView: TextView
var dateView: TextView
var menuImageView: ImageView
init {
typeImageView = root.findViewById(R.id.custom_item_type_image)
titleView = root.findViewById(R.id.custom_item_title)
dateView = root.findViewById(R.id.custom_item_date)
menuImageView = root.findViewById(R.id.custom_item_menu)
}
}
package com.tutorial.c34
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ArrayAdapter
import android.widget.Toast
import androidx.core.content.res.ResourcesCompat
class DriveAdapter(cxt: Context, val resId: Int, val data: MutableList<DriveVO>)
: ArrayAdapter<DriveVO>(cxt, resId){
override fun getCount(): Int {
return data.size
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
var view = convertView
if (view == null) {
val inflater = context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
as LayoutInflater
view = inflater.inflate(resId, null)
val holder = DriveHolder(view)
view!!.tag = holder
}
// ๋ทฐ ํ๋์์ ๋ทฐ ๊บผ๋ด๊ธฐ
val holder = view.tag as DriveHolder
val typeImageView = holder.typeImageView
val titleView = holder.titleView
val dateView = holder.dateView
val menuImageView = holder.menuImageView
// ๊ฐ ํญ๋ชฉ์ ์๋ ๋ทฐ๋ค์ ์์ฑ ๋ณ๊ฒฝ
val (type, title, date) = data[position]
titleView.text = title
dateView.text = date
// ๊ฐ ํญ๋ชฉ์ ํ์
์ ๋ฐ๋ผ ์ด๋ฏธ์ง ๋ณ๊ฒฝ
when (type) {
"doc" -> {
typeImageView.setImageDrawable(
ResourcesCompat.getDrawable(
context.resources,
R.drawable.ic_type_doc, null
)
)
}
"file" -> {
typeImageView.setImageDrawable(
ResourcesCompat.getDrawable(
context.resources,
R.drawable.ic_type_file, null
)
)
}
"img" -> {
typeImageView.setImageDrawable(
ResourcesCompat.getDrawable(
context.resources,
R.drawable.ic_type_image, null
)
)
}
}
// ๋ฉ๋ด ์ด๋ฏธ์ง ํด๋ฆญํ๋ฉด ํ ์คํธ ๋ฉ์์ง ๋จ๋๋ก
menuImageView.setOnClickListener {
Toast.makeText(context, "$title menu click", Toast.LENGTH_SHORT).show()
}
return view
}
}
MainActivity.kt
package com.tutorial.c34
import android.os.Bundle
import android.widget.ListView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val mutableList = mutableListOf<DriveVO>()
mutableList.add(DriveVO("doc", "์๋๋ก์ด๋", "2์ 6์ผ"))
mutableList.add(DriveVO("file", "db.zip", "2์ 6์ผ"))
mutableList.add(DriveVO("img", "exam.png", "2์ 6์ผ"))
val listView = findViewById<ListView>(R.id.custom_list)
val adapter = DriveAdapter(this, R.layout.custom_item, mutableList)
listView.adapter = adapter
}
}
<?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"
android:orientation="vertical"
android:padding="16dp"
tools:context=".MainActivity">
<Spinner
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:spinnerMode="dialog"/>
<AutoCompleteTextView
android:id="@+id/auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="language">
<item>ํ๊ตญ์ด</item>
<item>์์ด</item>
<item>์ค๊ตญ์ด</item>
<item>๋
์ผ์ด</item>
</string-array>
</resources>
package com.tutorial.c35
import android.os.Bundle
import android.widget.ArrayAdapter
import android.widget.AutoCompleteTextView
import android.widget.Spinner
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val spinner = findViewById<Spinner>(R.id.spinner)
val autoCompleteTextView = findViewById<AutoCompleteTextView>(R.id.auto)
val data = resources.getStringArray(R.array.language)
val adapter = ArrayAdapter(
this,
android.R.layout.simple_dropdown_item_1line,
data
)
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
spinner.adapter = adapter
val autoData = arrayOf("apply", "apple", "below", "coco")
val autoAdapter = ArrayAdapter(
this,
android.R.layout.simple_dropdown_item_1line,
autoData
)
autoCompleteTextView.setAdapter(autoAdapter)
}
}