Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[Android] ListView 복습 본문

인프런/[인프런] 앱 8개를 만들면서 배우는 안드로이드 코틀린

[Android] ListView 복습

Seung__ 2022. 3. 20. 12:21

Activity -> Adapter로 데이터 전달

Adapter는 각각의 item에 해당하는 view 생성하여 LIstView에 전달

 

1. Activity


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

        val sentenceList = mutableListOf<String>()
        sentenceList.add("검정화면에 대충 흰 글씨")
        sentenceList.add("명언1")
        sentenceList.add("명언2")
        sentenceList.add("명언3")
        sentenceList.add("명언4")

        val sentenceAdapter = ListViewAdapter(sentenceList)
        val listView = findViewById<ListView>(R.id.sentenceListView)

        //adapter 연결
        listView.adapter = sentenceAdapter

    }
}

 

2. Adapter


  • 가장 기본적으로 BaseAdapter를 상속
  • convertView는 각 item에 대항하는 layout을 View객체로 전환한 것.
  • LayoutInflater: Layout을 View객체로 변환.
class ListViewAdapter(val List: MutableList<String>): BaseAdapter() {
    override fun getCount(): Int {
       return List.size
    }

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

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

    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        //convertView: 각 item에 해당하는 view
        var convertView = convertView

        if (convertView == null){
            //layout을 객체화: itemView를 객체화
            convertView = LayoutInflater.from(parent?.context).inflate(R.layout.listview_item, parent, false)
        }

        val listViewText = convertView!!.findViewById<TextView>(R.id.listViewTextArea)
        listViewText.text = List[position]

        return convertView
    }
}

 

 

3. ListView에 들어갈 각 item에 해당하는 Layout


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="60sp">

    <TextView
        android:id="@+id/listViewTextArea"
        android:layout_margin="10sp"
        android:textSize="15sp"
        android:fontFamily="@font/bmjua_ttf"
        android:text="textArea"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>
</LinearLayout>

 

 

4. LIstView를 포함한 Layout


<?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=".SentenceActivity">

    <ListView
        android:id="@+id/sentenceListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

 

 

 

GitHub - bsh6463/InflearnGoodWords

Contribute to bsh6463/InflearnGoodWords development by creating an account on GitHub.

github.com

 

Comments