Learn & Record
Android (어댑터뷰, 리스트뷰, 동적으로 항목 추가, 그리드뷰) 본문
1. 어댑터뷰
- 하위 클래스를 사용하기에 어댑터뷰의 모양을 설정하고 데이터를 채워주는
- ArrayAdapter<T> 클래스를 함께 사용
2. 리스트뷰
<?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"
tools:context=".MainActivity">
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package kr.jeongmo.a0510_project
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "리스트뷰 테스트"
val listView = findViewById<View>(R.id.listView1) as ListView
val mid = arrayOf("홍콩", "타이페이", "타이난", "가오슝", "후쿠오카",
"쿄토", "오사카", "구마모토", "히로시마", "하노이",
"호치민", "다낭", "하롱베이", "싱가폴", "마카오")
val adapter: ArrayAdapter<String> = ArrayAdapter(this,
android.R.layout.simple_list_item_1, mid)
listView.adapter = adapter
listView.setOnItemClickListener { adapterView, view, i, l ->
Toast.makeText(applicationContext, mid[i], Toast.LENGTH_SHORT).show()
}
}
}
android.R.layout.simple_list_item_multiple_choice, mid)
listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE
- 추가로 디자인 변경 가능
2. 동적으로 항목 추가
<?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"
tools:context=".MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editItem"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnAdd"
android:text="항목추가"/>
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package kr.jeongmo.a0510_project
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.*
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "리스트뷰 테스트"
val listView = findViewById<View>(R.id.listView1) as ListView
val mid = arrayOf("홍콩", "타이페이", "타이난", "가오슝", "후쿠오카",
"쿄토", "오사카", "구마모토", "히로시마", "하노이",
"호치민", "다낭", "하롱베이", "싱가폴", "마카오")
val midList = mutableListOf<String>()
val adapter: ArrayAdapter<String> = ArrayAdapter(this,
android.R.layout.simple_list_item_multiple_choice, mid)
listView.choiceMode = ListView.CHOICE_MODE_MULTIPLE
listView.adapter = adapter
val editItem = findViewById<EditText>(R.id.editItem)
val btnAdd = findViewById<Button>(R.id.btnAdd)
btnAdd.setOnClickListener {
midList.add(editItem.text.toString())
adapter.notifyDataSetChanged()
}
listView.setOnItemLongClickListener { parent, view, position, id ->
midList.removeAt(position);
adapter.notifyDataSetChanged()
false
}
listView.setOnItemClickListener { adapterView, view, i, l ->
Toast.makeText(applicationContext, mid[i], Toast.LENGTH_SHORT).show()
}
}
}
3,. 그리드뷰
- 프로젝트 새로 생성 후 > dialog.xml 파일 추가
<?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"
tools:context=".MainActivity">
<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:numColumns="4"/>
</LinearLayout>
- activity_main.xml 파일 코드
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageViewPoster"/>
</LinearLayout>
- dialog.xml 파일 코드
package kr.jeongmo.a0510_project_gridview
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.GridView
import android.widget.ImageView
import androidx.appcompat.app.AlertDialog
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
title = "그리드뷰 영화 포스터"
val gridView = findViewById<GridView>(R.id.gridView)
val myGridAdapter = MyGridAdapter(this)
gridView.adapter = myGridAdapter
}
inner class MyGridAdapter(val context: Context): BaseAdapter() {
val posterId = arrayOf(
R.drawable.mov01,R.drawable.mov02,R.drawable.mov03,R.drawable.mov04,
R.drawable.mov05,R.drawable.mov06,R.drawable.mov07,R.drawable.mov08,
R.drawable.mov09,R.drawable.mov10,R.drawable.mov11,R.drawable.mov12,
R.drawable.mov13,R.drawable.mov14,R.drawable.mov15,R.drawable.mov16,
R.drawable.mov17,R.drawable.mov18,R.drawable.mov19,R.drawable.mov20,
R.drawable.mov21,R.drawable.mov22,R.drawable.mov23,R.drawable.mov24,
R.drawable.mov25,R.drawable.mov26,R.drawable.mov27 ,R.drawable.mov28,
R.drawable.mov29,R.drawable.mov30,R.drawable.mov31,R.drawable.mov32,
R.drawable.mov33,R.drawable.mov34,R.drawable.mov35,R.drawable.mov36,
R.drawable.mov37,R.drawable.mov38,R.drawable.mov39,R.drawable.mov40,
R.drawable.mov41,R.drawable.mov42,R.drawable.mov43,R.drawable.mov40,
)
override fun getCount(): Int {
return posterId.size
}
override fun getItem(p0: Int): Any {
return 0
}
override fun getItemId(p0: Int): Long {
return 0
}
override fun getView(p0: Int, p1: View?, p2: ViewGroup?): View {
val imageView = ImageView(context)
imageView. layoutParams = ViewGroup.LayoutParams(200, 300)
imageView.scaleType = ImageView.ScaleType.FIT_CENTER
imageView.setPadding(5, 5, 5, 5)
imageView.setImageResource(posterId[p0])
imageView.setOnClickListener {
val dialogView = View.inflate(this@MainActivity, R.layout.dialog, null)
val dialog = AlertDialog.Builder(this@MainActivity)
val imageViewPoster = dialogView.findViewById<ImageView>(R.id.imageViewPoster)
imageViewPoster.setImageResource(posterId[p0])
dialog.setTitle("큰 포스터")
dialog.setIcon(R.drawable.ic_launcher_foreground)
dialog.setView(dialogView)
dialog.setNegativeButton("닫기", null)
dialog.show()
}
return imageView
}
}
}
- 액티비티 파일 코드
공부 과정을 정리한 것이라 내용이 부족할 수 있습니다.
부족한 내용은 추가 자료들로 보충해주시면 좋을 것 같습니다.
읽어주셔서 감사합니다 :)
'Dev > Android' 카테고리의 다른 글
[Android 오류_해결 5가지] the emulator process for avd has terminated (2) | 2024.05.22 |
---|---|
Android (갤러리, 스피너, 구글 지도, 구글 클라우드, API 연동, 아이콘 생성 ) (0) | 2024.05.13 |
Android (액티비티 생명주기, 로그캣, 액티비티 테스트, 안드로이드 4대 구성요소, 디자인탭 활용, 액티비티와 프래그먼트) (0) | 2024.05.09 |
Android (양방향 액티비티, 암시적 인텐트, 액티비티와 인텐트) (0) | 2024.05.08 |
Android (명화 선호도 투표 프로젝트 기능 추가 ) (0) | 2024.05.07 |