ListView 과정
1. 메인액티비티에서 데이터를 어댑터(Adapter)에 보내주기
2. 어댑터(Adapter)에서 아이템을 listview_item.xml에 하나하나씩 넣어준다
3. 최종적으로 메인택티비티에 있는 listview에 넣어준다
어댑터(Adapter)
- 데이터와 뷰를 짝지어 준다
- 요청시 해당 번째 데이터를 준다
- 데이터가 변경되었을 때 갱신해준다
어댑터(Adapter)의 종류
BaseAdapter(기본형태)
- ArrayAdapter : 배열 타입
- CursorAdapter : 데이터 전용
- SimpleAdapter : 정적 XML
나머지들은 BaseAdapter을 상속받는다(나머지들은 잘 사용안한다)
1. 메인액티비티에 listview만들어주기
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
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/mainListView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.appcompat.widget.LinearLayoutCompat>
2. 어답터 만들기 (new- kotlin class/File)
// 문자열 데이터를 가진 리스트를 받는다
// BaseAdapter()를 상속 받는다
class ListViewAdapter(val List : MutableList<String>) : BaseAdapter() {
// 필요한 메서드를 override를 해주기
override fun getCount(): Int {
// 전체 데이터의 크기(갯수) 리턴
return List.size
}
override fun getItem(position: Int): Any {
// 전체 데이터 중에서 해당번째(position)의 데이터를 가져다 준다
return List[position]
}
override fun getItemId(position: Int): Long {
return position.toLong()
}
override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
// 해당 번째 뷰를 리턴
// listview_item 연결하기
var convertView = convertView
if (convertView == null) {
convertView = LayoutInflater.from(parent?.context).inflate(R.layout.listview_item, parent, false)
}
val title = convertView!!.findViewById<TextView>(R.id.listviewItem)
// title의 text을 List안에 있는 아이템으로(우리가 만든 A,B,C) 연결해주기
title.text = List[position]
return convertView!!
}
}
3. listview_item.xm 만들기
<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80dp">
<TextView
android:id="@+id/listviewItem"
android:text="리스트뷰 아이템"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</androidx.appcompat.widget.LinearLayoutCompat>
4. MainActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// list_item에 데이터를 생성성
val list_item = mutableListOf<String>()
list_item.add("A")
list_item.add("B")
list_item.add("C")
// 생성한 데이터를 어댑터에 넘겨주기
val Myadapter = ListViewAdapter(list_item)
// 우리가 만든 ListView 찾기
val lv = findViewById<ListView>(R.id.mainListView)
// lv에 있는 어댑터에 우리가 만든 Myadapter(ListViewAdapter)에 연결해주기
lv.adapter = Myadapter
}
}
- 데이터를 생성한다
- 생성한 데이터를 어댑터에 넘겨주기
- 우리가 만든 ListView찾기(메인액티비티에 있는 listview)
- 어댑터 연결하기
추가적으로
데이터 모델(데이터 덩어리)을 만들어서 데이터를 추가할 수 있다
1. ListModel 파일 만들기
data class ListModel (
val title1 : String,
val title2 : String
)
2. MainActivity 코드 수정
val list_item = mutableListOf<ListModel>()
list_item.add(ListModel("A", "B"))
list_item.add(ListModel("C", "D"))
list_item.add(ListModel("E", "F"))
3. ListViewAdapter 코드 수정
class ListViewAdapter(val List : MutableList<ListModel>) : BaseAdapter()
val title = convertView!!.findViewById<TextView>(R.id.listviewItem)
val title2 = convertView!!.findViewById<TextView>(R.id.listviewItem2)
// title의 text을 List안에 있는 아이템으로(우리가 만든 A,B,C) 연결해주기
title.text = List[position].title1
title2.text = List[position].title2
4. listview_item.xm 코드 추가
<TextView
android:id="@+id/listviewItem2"
android:text="리스트뷰 아이템"
android:layout_margin="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
참고자료
https://b.jy.is/android-layoutinflater/
안드로이드 LayoutInflater 사용하기
안드로이드에서 레이아웃 XML파일을 View객체로 만들기 위해서는 LayoutInflater를 이용한다. LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); View view = inflater.inflate(R.layout
b.jy.is
https://developer.android.com/reference/android/view/LayoutInflater
LayoutInflater | Android Developers
android.net.wifi.hotspot2.omadm
developer.android.com
'Android > 개념' 카테고리의 다른 글
[안드로이드]JSON 파일 저장 및 파싱 이용해서 리사이클러뷰(RecyclerView)에 적용하기 (0) | 2023.04.02 |
---|---|
[안드로이드]Navigation Component (1) | 2023.03.10 |
[안드로이드] BottomNavigationView (0) | 2023.03.09 |
RecyclerView (0) | 2022.10.10 |
데이터 바인딩 (0) | 2022.10.08 |