■ 初期設定
* Realm (DB) と Anko (ページ移動) を追加する => Realm と Anko については、以下の関連記事を参照のことRealm
https://blogs.yahoo.co.jp/dk521123/37714626.html
Anko
https://blogs.yahoo.co.jp/dk521123/32366977.html
./build.gradle
buildscript { ext.kotlin_version = '1.2.50' ext.anko_version = '0.10.5' // ★ Add ★ // ... 略 ... dependencies { classpath 'com.android.tools.build:gradle:3.1.4' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" classpath "io.realm:realm-gradle-plugin:5.4.1" // ★ Add ★ }
./app/build.gradle
apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-kapt' // ★ Add ★ apply plugin: 'realm-android' // ★ Add ★ // ... 略 ... dependencies { // ... 略 ... implementation "org.jetbrains.anko:anko:$anko_version" // ★ Add ★ implementation 'io.realm:android-adapters:2.1.1' // ★ Add:今回で初めて追加 ★
■ サンプル
https://blogs.yahoo.co.jp/dk521123/37714626.htmlをベースにする。 「DemoApplication.kt」「AndroidManifest.xml」「Comment.kt」は、 同じなので、上記の関連記事を参照。
CommentListViewAdapter.kt
import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import android.widget.TextView import io.realm.OrderedRealmCollection import io.realm.RealmBaseAdapter class CommentListViewAdapter(data: OrderedRealmCollection<Comment>?) : RealmBaseAdapter<Comment>(data) { inner class ViewHolder(cell: View) { val id = cell.findViewById<TextView>(android.R.id.text1) val title= cell.findViewById<TextView>(android.R.id.text2) } override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { val view: View val viewHolder: ViewHolder when (convertView) { null -> { val layoutInflater = LayoutInflater.from(parent?.context) view = layoutInflater.inflate(android.R.layout.simple_list_item_2, parent, false) viewHolder = this.ViewHolder(view) view.tag = viewHolder } else -> { view = convertView viewHolder = view.tag as ViewHolder } } adapterData?.run { val comment = get(position) viewHolder.id.text = comment.id.toString() viewHolder.title.text = comment.title } return view } }
ListViewActivity.kt
import android.support.v7.app.AppCompatActivity import android.os.Bundle import io.realm.Realm import kotlinx.android.synthetic.main.activity_list_view.* import org.jetbrains.anko.startActivity class ListViewActivity : AppCompatActivity() { private lateinit var realm: Realm override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_list_view) this.realm = Realm.getDefaultInstance() val comments = realm.where(Comment::class.java).findAll().sort("id", Sort.ASCENDING) this.listView.adapter = CommentListViewAdapter(comments) this.listView.setOnItemClickListener { parent, view, position, id -> val comment = parent.getItemAtPosition(position) as Comment this.startActivity<EditCommentActivity>("comment_id" to comment.id) } } override fun onDestroy() { super.onDestroy() this.realm.close() } }
EditCommentActivity.kt
import android.support.v7.app.AppCompatActivity import android.os.Bundle import android.view.View import android.widget.Toast import io.realm.Realm import io.realm.kotlin.createObject import io.realm.kotlin.where import kotlinx.android.synthetic.main.activity_edit_comment.* class EditCommentActivity : AppCompatActivity() { private lateinit var realm: Realm override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) this.setContentView(R.layout.activity_edit_comment) this.realm = Realm.getDefaultInstance() val commentId = intent?.getLongExtra("comment_id", -1L) if (commentId != -1L) { val comment = realm.where<Comment>() .equalTo("id", commentId).findFirst() this.resultIdTextView.text = comment?.id.toString() this.resultTitleEditText.setText(comment?.title) this.resultDetailEditText.setText(comment?.detail) this.deleteButton.visibility = View.VISIBLE } else { this.deleteButton.visibility = View.INVISIBLE } this.saveButton.setOnClickListener { it: View? -> when (commentId) { -1L -> { try { realm.executeTransaction { val maxId = realm.where<Comment>().max("id") val targetId = (maxId?.toLong() ?: 0L) + 1L val comment = realm.createObject<Comment>(targetId) comment.title = this.resultTitleEditText.text.toString() comment.detail = this.resultDetailEditText.text.toString() Toast.makeText(this, "追加しました. ID : " + targetId.toString(), Toast.LENGTH_SHORT).show() } } catch (ex: Exception) { ex.printStackTrace() } } else -> { try { realm.executeTransaction { val comment = realm.where<Comment>() .equalTo("id", commentId).findFirst() comment?.title = resultTitleEditText.text.toString() comment?.detail = resultDetailEditText.text.toString() Toast.makeText(this, "修正しました.", Toast.LENGTH_SHORT).show() } } catch (ex: Exception) { ex.printStackTrace() } } } } this.deleteButton.setOnClickListener { realm.executeTransaction { realm.where<Comment>().equalTo("id", commentId)?.findFirst()?.deleteFromRealm() Toast.makeText(this, "削除しました.", Toast.LENGTH_SHORT).show() } } } override fun onDestroy() { super.onDestroy() realm.close() } }