Commit 7951025f authored by Andres Käver's avatar Andres Käver

db, scrollview

parent 526b8a2b
......@@ -30,6 +30,7 @@ dependencies {
implementation 'androidx.core:core-ktx:1.3.1'
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation 'androidx.constraintlayout:constraintlayout:2.0.1'
implementation 'androidx.recyclerview:recyclerview:1.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
......
package ee.taltech.mobile2020.dbdemo
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.row_view.view.*
class DataRecyclerViewAdapter(private val context: Context, private val repo: PersonRepository) : RecyclerView.Adapter<DataRecyclerViewAdapter.ViewHolder>() {
lateinit var dataSet: List<Person>
fun refreshData() {
dataSet = repo.getAll()
}
init {
refreshData()
}
private val inflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val rowView = inflater.inflate(R.layout.row_view, parent, false)
return ViewHolder(rowView)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val person = dataSet.get(position)
holder.itemView.textViewId.text = person.id.toString()
holder.itemView.textViewFirstName.text = person.firstName
holder.itemView.textViewLastName.text = person.lastName
}
override fun getItemCount(): Int {
return dataSet.count()
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView)
}
\ No newline at end of file
......@@ -3,24 +3,44 @@ package ee.taltech.mobile2020.dbdemo
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
private lateinit var personRepo: PersonRepository
private lateinit var adapter: RecyclerView.Adapter<*>
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
personRepo = PersonRepository(this).open()
personRepo.add(Person("Andres", "Käver"))
personRepo.add(Person("Kerman", "Saapar"))
val persons = personRepo.getAll()
recylerViewPersons.layoutManager = LinearLayoutManager(this)
adapter = DataRecyclerViewAdapter(this, personRepo)
recylerViewPersons.adapter = adapter
}
fun buttonAddPersonOnClick(view: View) {
personRepo.add(
Person(
editTextTextPersonFirstName.text.toString(),
editTextTextPersonLastName.text.toString()
)
)
(adapter as DataRecyclerViewAdapter).refreshData()
adapter.notifyDataSetChanged()
}
persons.forEach { person ->
Log.d("Person", person.id.toString() + " " + person.firstName + " " + person.lastName)
}
override fun onDestroy() {
super.onDestroy()
personRepo.close()
}
}
\ No newline at end of file
......@@ -6,13 +6,47 @@
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
<EditText
android:id="@+id/editTextTextPersonFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editTextTextPersonLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonFirstName" />
<Button
android:id="@+id/buttonAddPerson"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="32dp"
android:onClick="buttonAddPersonOnClick"
android:text="Add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/editTextTextPersonLastName" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recylerViewPersons"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextTextPersonLastName" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
<?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="64dp">
<TextView
android:id="@+id/textViewId"
android:layout_width="32dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewFirstName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textViewId"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textViewLastName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/textViewFirstName"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment