badazim’s blog

素人がプログラミングを勉強しながら覚え書きを書きます

kotlinにおけるArrayのNullチェック

kotlinのArrayのNullチェックで躓いたのですが、とりあえず解決したので、メモ。。

 

Array<String>? のnull許容の意味がわからず、これでコンパイルエラーになっていた。

private fun arraytest(testArray:Array<String>?){
for(i in 0..5){
val ELEMENT = testArray[i]
if (ELEMENT != null) {
val element:String = ELEMENT
}
}
}

→Only safe (?.) or non-null asserted (!!.) calls are allowed on a nullable receiver of type Array<String>?

 

Array<String>? の場合は配列の中身ではなく、配列自体をnull判定すれば扱えるみたい。

private fun arraytest(testArray:Array<String>?){
if (testArray != null){
for(i in 0..5){
val element:String = testArray[i]
}
}
}

コンパイルは通った。

 

ちなみにnull許容型 Array<String?>の場合も勘違いしていた。配列に添字を付けてnull判定しても、スマートキャストされない模様。。

private fun arraytest(testArray:Array<String?>){
for(i in 0..5){
if (testArray[i] != null) {
val element:String = testArray[i]
}
}
}

→Type mismatch: inferred type is String? but String was expected

 

以下の書き方ならコンパイルが通る。

private fun arraytest(testArray:Array<String?>){
for(i in 0..5){
val ELEMENT = testArray[i]
if (ELEMENT != null) {
val element:String = ELEMENT
}
}
}

kotlinでRecyclerViewを使ってみる

kotlinでRecyclerViewを使ってみた。各種サイトに色々情報が乗っているものの、ど素人の自分が動かせるまでには多少の苦労(修正)が必要だったので、めも。。。。

 

activityのxml(activity_order_change2.xml)

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

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/MyRecyclerView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="150dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

 

1行あたりのデータ形式(row.xml)

<?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="wrap_content"
android:layout_margin="30dp">

<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />

<TextView
android:id="@+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>

 

ktファイル

package com.example.myfiveninepractice

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import kotlinx.android.synthetic.main.activity_order_change2.*
import kotlinx.android.synthetic.main.row.view.*

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

MyRecyclerView.setHasFixedSize(true)
MyRecyclerView.adapter = MyRecyclerViewAdapter(this.createDataset())
MyRecyclerView.layoutManager = LinearLayoutManager(this)

val itemTouchHelper = ItemTouchHelper(object: ItemTouchHelper.SimpleCallback(ItemTouchHelper.UP or ItemTouchHelper.DOWN,ItemTouchHelper.LEFT){
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
val fromPostion = viewHolder?.adapterPosition ?:0
val toPosition = target?.adapterPosition ?:0

(MyRecyclerView.adapter as MyRecyclerViewAdapter).notifyItemMoved(fromPostion,toPosition)

return true
}

override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
viewHolder?.let{
(MyRecyclerView.adapter as MyRecyclerViewAdapter).notifyItemRemoved(viewHolder.adapterPosition)
}
}
})
itemTouchHelper.attachToRecyclerView(MyRecyclerView)
}


private fun createDataset(): List<RowData> {
val dataset: MutableList<RowData> = ArrayList()
for (i in 0..3) {
val data = RowData("テスト" + i + "人目","テストは" + i + "点でした")
dataset.add(data)
}
return dataset
}

}

class RowData(var title: String,var detail:String)

public class MyViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
var titleView = itemView.title
var detailView = itemView.detail
}

class MyRecyclerViewAdapter (private val list:List<RowData>):RecyclerView.Adapter<MyViewHolder>(){
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val layoutInflater = LayoutInflater.from(parent.context) //ここがparentになる理由
val mView = layoutInflater.inflate(R.layout.row, parent, false)
return MyViewHolder(mView)
}

override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.titleView.text = list[position].title
holder.detailView.text = list[position].detail
}

override fun getItemCount(): Int {
return list.size
}
}