mirror of
https://github.com/Xevion/simple-todo.git
synced 2025-12-06 01:16:23 -06:00
Implement adapter for RecyclerView connected to list of strings
This commit is contained in:
@@ -2,10 +2,23 @@ package com.embers.simpleto_do
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity
|
||||
import android.os.Bundle
|
||||
import android.widget.Button
|
||||
import androidx.recyclerview.widget.LinearLayoutManager
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class MainActivity : AppCompatActivity() {
|
||||
val taskList = mutableListOf<String>()
|
||||
|
||||
override fun onCreate(savedInstanceState: Bundle?) {
|
||||
super.onCreate(savedInstanceState)
|
||||
setContentView(R.layout.activity_main)
|
||||
|
||||
findViewById<Button>(R.id.button).setOnClickListener {
|
||||
}
|
||||
|
||||
val recyclerView = findViewById<RecyclerView>(R.id.recyclerView)
|
||||
val adapter = TaskItemAdapter(taskList)
|
||||
recyclerView.adapter = adapter
|
||||
recyclerView.layoutManager = LinearLayoutManager(this)
|
||||
}
|
||||
}
|
||||
32
app/src/main/java/com/embers/simpleto_do/TaskItemAdapter.kt
Normal file
32
app/src/main/java/com/embers/simpleto_do/TaskItemAdapter.kt
Normal file
@@ -0,0 +1,32 @@
|
||||
package com.embers.simpleto_do
|
||||
|
||||
import android.view.LayoutInflater
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.TextView
|
||||
import androidx.recyclerview.widget.RecyclerView
|
||||
|
||||
class TaskItemAdapter(private val taskList: List<String>) :
|
||||
RecyclerView.Adapter<TaskItemAdapter.ViewHolder>() {
|
||||
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
|
||||
val textView: TextView = itemView.findViewById(android.R.id.text1)
|
||||
}
|
||||
|
||||
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
|
||||
val context = parent.context
|
||||
val inflater = LayoutInflater.from(context)
|
||||
val taskView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false)
|
||||
|
||||
return ViewHolder(taskView)
|
||||
}
|
||||
|
||||
override fun getItemCount(): Int {
|
||||
return taskList.size
|
||||
}
|
||||
|
||||
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
|
||||
val task: String = taskList[position]
|
||||
val textView = holder.textView
|
||||
textView.text = task
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,7 @@
|
||||
app:layout_constraintStart_toStartOf="parent" />
|
||||
|
||||
<androidx.recyclerview.widget.RecyclerView
|
||||
android:id="@+id/recyclerView"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="0dp"
|
||||
app:layout_constraintBottom_toTopOf="@+id/editTodoText"
|
||||
|
||||
Reference in New Issue
Block a user