Commit a22cd23c by Paktalin

Entries are added to the DB in onPause method

parent 81afc171
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
package com.paktalin.wordbook.database
import android.content.ContentValues
import android.provider.BaseColumns._ID
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_WORD
import com.paktalin.wordbook.database.DatabaseEntries.TABLE_NAME
import com.paktalin.wordbook.log
import com.paktalin.wordbook.ui.Vocabulary
class DataManager {
companion object {
fun loadVocabulary(dbHelper: DatabaseHelper): Vocabulary {
val db = dbHelper.writableDatabase
val cursor = db.query(DatabaseEntries.TABLE_NAME, arrayOf(DatabaseEntries.COLUMN_WORD, DatabaseEntries.COLUMN_TRANSLATION), null, null, null, null, null)
val db = dbHelper.readableDatabase
val cursor = db.query(TABLE_NAME, arrayOf(COLUMN_WORD, COLUMN_TRANSLATION, _ID), null, null, null, null, null)
val vocabulary = Vocabulary()
with(cursor) {
while (moveToNext()) {
val word = getString(getColumnIndexOrThrow(DatabaseEntries.COLUMN_WORD))
val translation = getString(getColumnIndexOrThrow(DatabaseEntries.COLUMN_TRANSLATION))
val word = getString(getColumnIndexOrThrow(COLUMN_WORD))
val translation = getString(getColumnIndexOrThrow(COLUMN_TRANSLATION))
val id = getLong(getColumnIndexOrThrow(_ID))
vocabulary.add(word, translation, id)
}
}
cursor.close()
vocabulary.print()
return vocabulary
}
// fun replaceEntry()
fun updateVocabulary(dbHelper: DatabaseHelper, vocabulary: Vocabulary, updatedPositions: MutableSet<Int>) {
val db = dbHelper.writableDatabase
for (i in updatedPositions) {
val values = ContentValues().apply {
put(COLUMN_WORD, vocabulary[i].word)
put(COLUMN_TRANSLATION, vocabulary[i].translation)
}
if (vocabulary[i].id != (-1).toLong())
db?.update(TABLE_NAME, values, "$_ID=${vocabulary[i].id}", null)
else
log(db?.insert(TABLE_NAME, null, values).toString())
}
loadVocabulary(dbHelper)
}
}
}
......@@ -10,6 +10,6 @@ class Entry(var word: String, var translation: String) {
}
fun print() {
log("$word - $translation")
log("$word - $translation - $id")
}
}
package com.paktalin.wordbook.ui
import android.content.ContentValues
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import com.paktalin.wordbook.R
import com.paktalin.wordbook.database.DataManager.Companion.loadVocabulary
import com.paktalin.wordbook.database.DataManager.Companion.updateVocabulary
import com.paktalin.wordbook.database.DatabaseHelper
import com.paktalin.wordbook.log
import kotlinx.android.synthetic.main.activity_main.*
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_WORD
import com.paktalin.wordbook.database.DatabaseEntries.TABLE_NAME
class MainActivity : AppCompatActivity() {
lateinit var vocabulary: Vocabulary
private val dbHelper = DatabaseHelper(this@MainActivity)
private lateinit var adapter: VocabularyAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
vocabulary = loadVocabulary(DatabaseHelper(this@MainActivity))
override fun onResume() {
super.onResume()
val vocabulary = loadVocabulary(dbHelper)
adapter = VocabularyAdapter(vocabulary)
recycler_view.apply {
layoutManager = LinearLayoutManager(this@MainActivity)
adapter = VocabularyAdapter(vocabulary)
adapter = this@MainActivity.adapter
}
}
fun fillDb() {
val dbHelper = DatabaseHelper(this@MainActivity)
val db = dbHelper.writableDatabase
val values = ContentValues().apply {
put(COLUMN_WORD, "ragazzo")
put(COLUMN_TRANSLATION, "boy")
}
db?.insert(TABLE_NAME, null, values)
override fun onPause() {
super.onPause()
updateVocabulary(dbHelper, adapter.vocabulary, adapter.updatedPositions)
}
}
package com.paktalin.wordbook.ui
import android.text.Editable
import android.text.TextWatcher
import com.paktalin.wordbook.log
class MyTextWatcher(private val position: Int) : TextWatcher {
override fun afterTextChanged(editable: Editable?) {
log("$position")
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
}
}
\ No newline at end of file
......@@ -9,9 +9,4 @@ import kotlinx.android.synthetic.main.fragment_line.view.*
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val wordEt: EditText = itemView.word
val translationEt: EditText = itemView.translation
init {
// wordEt.addTextChangedListener(MyTextWatcher())
// translationEt.addTextChangedListener(MyTextWatcher())
}
}
\ No newline at end of file
......@@ -12,6 +12,18 @@ class Vocabulary: Iterable<Entry>{
return entries.size
}
fun updateWord(position: Int, word: String) {
entries[position].word = word
}
fun updateTranslation(position: Int, translation: String) {
entries[position].translation = translation
}
fun print() {
for (entry in this) entry.print()
}
override fun iterator(): Iterator<Entry> {
return entries.iterator()
}
......
package com.paktalin.wordbook.ui
import android.support.v7.widget.RecyclerView
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.ViewGroup
import com.paktalin.wordbook.R
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_TRANSLATION
import com.paktalin.wordbook.database.DatabaseEntries.COLUMN_WORD
class VocabularyAdapter(private val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewHolder>() {
class VocabularyAdapter(val vocabulary: Vocabulary) : RecyclerView.Adapter<ViewHolder>() {
var updatedPositions = mutableSetOf<Int>()
override fun getItemCount(): Int {
return vocabulary.size()
......@@ -14,7 +19,8 @@ class VocabularyAdapter(private val vocabulary: Vocabulary) : RecyclerView.Adapt
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
holder.wordEt.setText(vocabulary[position].word)
holder.translationEt.setText(vocabulary[position].translation)
holder.wordEt.addTextChangedListener(MyTextWatcher(position))
holder.wordEt.addTextChangedListener(MyTextWatcher(position, COLUMN_WORD))
holder.translationEt.addTextChangedListener(MyTextWatcher(position, COLUMN_TRANSLATION))
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
......@@ -22,4 +28,17 @@ class VocabularyAdapter(private val vocabulary: Vocabulary) : RecyclerView.Adapt
return ViewHolder(view)
}
inner class MyTextWatcher(private val position: Int, private val column: String) : TextWatcher {
override fun afterTextChanged(editable: Editable?) {
val updatedField = editable?.toString()
if (updatedField != null) {
if (column == COLUMN_WORD) vocabulary.updateWord(position, updatedField)
else vocabulary.updateTranslation(position, updatedField)
updatedPositions.add(position)
}
}
override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {}
}
}
\ 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 sign in to comment