Kotlin玩Android第03篇 Listview(01 ArrayAdapter)
本篇文章將會稍微提到Adapter的功用,
並且會用到Anko的onClick和alert來簡化App的程式碼。
本文提到的ArrayAdapter是可以直接拿來用的類別,無需由其他物件繼承便可以使用。
除非你的ListView構造比較複雜,不然我覺得今天這篇文章講的對想使用ListView的人,使用ArrayAdapter也算是有點小用(我不是在沒罵人😂)。
實作的影片!
Adapter本身算是listview跟資料來源的橋樑,如果你是實際寫個繼承BaseAdapter的類別並實際去override裡面的方法,
就會明白連個getItem或getItemId都得自己去定義,但在此例ArrayAdapter同時也扮演著資料來源的角色,
所以我們省去了override的麻煩。
然後宣告ArrayAdapter必須定義好item的資料型態,此例為String型態。
如上面程式碼片段可以直接用.addAll()一次添加多個字串。
然後再添加底下這行
可惡!好像ArrayAdapter的用法太簡單反而沒什麼可講XD
畢竟還用到了Anko Layout的 onClick和alert這類工具函數,
可能就剩下基本的ArrayAdapter的自帶函數可講。
完整的activity_main.xml
本文提到的ArrayAdapter是可以直接拿來用的類別,無需由其他物件繼承便可以使用。
除非你的ListView構造比較複雜,不然我覺得今天這篇文章講的對想使用ListView的人,使用ArrayAdapter也算是有點小用(我不是在沒罵人😂)。
實作的影片!
Adapter本身算是listview跟資料來源的橋樑,如果你是實際寫個繼承BaseAdapter的類別並實際去override裡面的方法,
就會明白連個getItem或getItemId都得自己去定義,但在此例ArrayAdapter同時也扮演著資料來源的角色,
所以我們省去了override的麻煩。
然後宣告ArrayAdapter必須定義好item的資料型態,此例為String型態。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val myAdapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) | |
myAdapter.addAll("三個鉛筆", "四個腳踏車") |
然後再添加底下這行
main_listview.adapter = myAdapter,這三行就讓我們定義的listView跟ArrayAdapter連接起來,
然後資料便會自動地顯示在listView上。
畢竟還用到了Anko Layout的 onClick和alert這類工具函數,
可能就剩下基本的ArrayAdapter的自帶函數可講。
ArrayAdapter<T>.getItem(position: Int)
功用: 取得某個位置的值,用整數索引當作參數
ArrayAdapter<T>.remove(某個值: T)
那個某個值的型態是依據你ArrayAdapter當初建構T的型態而定,
在範例中是String型態。
功用: 從ArrayAdapter中移除某個值
ArrayAdapter<T>.clear()
功用: 清空ArrayAdapter中所有值。
完整的mainactivity.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.listviewdemo | |
import android.support.v7.app.AppCompatActivity | |
import android.os.Bundle | |
import android.widget.ArrayAdapter | |
import kotlinx.android.synthetic.main.activity_main.* | |
import org.jetbrains.anko.alert | |
import org.jetbrains.anko.noButton | |
import org.jetbrains.anko.sdk25.coroutines.onClick | |
import org.jetbrains.anko.yesButton | |
class MainActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContentView(R.layout.activity_main) | |
val myAdapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1) | |
myAdapter.addAll("三個鉛筆", "四個腳踏車") | |
// 當'新增'按鈕被點擊時 | |
btn_add.onClick { | |
val msg = msg_input.text.toString() | |
if (msg != "")// 如果'輸入字串'不為空字串,將'輸入字串'插入到myAdapter內 | |
myAdapter.insert(msg, 0) | |
} | |
// 當'清空'按鈕被點擊時 | |
btn_clear.onClick { | |
alert ("確定要刪除全部item嗎?"){ | |
yesButton { myAdapter.clear() } | |
noButton { } | |
}.show() | |
} | |
// 當main_listview中某個item被點擊時 | |
main_listview.setOnItemClickListener { adapterView, view, i, l -> | |
val msg = myAdapter.getItem(i) | |
alert ("確定要刪除$msg"){ | |
yesButton { myAdapter.remove(msg) } | |
noButton { } | |
}.show() | |
} | |
main_listview.adapter = myAdapter | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<android.support.constraint.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="com.example.listviewdemo.MainActivity"> | |
<EditText | |
android:id="@+id/msg_input" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:layout_marginTop="16dp" | |
android:ems="10" | |
android:hint="請輸入訊息" | |
android:inputType="textPersonName" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
<Button | |
android:id="@+id/btn_add" | |
android:layout_width="56dp" | |
android:layout_height="wrap_content" | |
android:layout_marginStart="16dp" | |
android:text="新增" | |
app:layout_constraintStart_toEndOf="@+id/msg_input" | |
app:layout_constraintTop_toTopOf="@+id/msg_input" /> | |
<ListView | |
android:id="@+id/main_listview" | |
android:layout_width="352dp" | |
android:layout_height="271dp" | |
android:layout_marginBottom="16dp" | |
android:layout_marginTop="16dp" | |
app:layout_constraintBottom_toBottomOf="parent" | |
app:layout_constraintLeft_toLeftOf="parent" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintTop_toBottomOf="@+id/msg_input" /> | |
<Button | |
android:id="@+id/btn_clear" | |
android:layout_width="55dp" | |
android:layout_height="wrap_content" | |
android:layout_marginRight="16dp" | |
android:layout_marginStart="16dp" | |
android:text="清空" | |
app:layout_constraintRight_toRightOf="parent" | |
app:layout_constraintStart_toEndOf="@+id/btn_add" | |
app:layout_constraintTop_toTopOf="@+id/btn_add" /> | |
</android.support.constraint.ConstraintLayout> |
個人心得:
ListView我還在研究能否用更複雜的呈現,
所以之後將會撰寫較複雜的ListView內容的程式碼,
可能也順便在寫個文章。
另外距離上一篇又寫了一陣子,Anko Layout其實還是蠻適合跟原本的XML整合的,因為那幾天都還在逞強能否只用Anko Layout定義視圖反而浪費了許多學習時間,仔細去查安卓還有很多可以玩的。
我希望能把一些東西或是程式碼紀錄能詳細一點,
這樣之後在翻閱筆記時才不用在額外多費時間找資料,
這也是為什麼每篇文我會盡量拍影片的原因,因為怕我文章寫的流程或說明不夠完整😂😂😂😂。
可能也順便在寫個文章。
另外距離上一篇又寫了一陣子,Anko Layout其實還是蠻適合跟原本的XML整合的,因為那幾天都還在逞強能否只用Anko Layout定義視圖反而浪費了許多學習時間,仔細去查安卓還有很多可以玩的。
我希望能把一些東西或是程式碼紀錄能詳細一點,
這樣之後在翻閱筆記時才不用在額外多費時間找資料,
這也是為什麼每篇文我會盡量拍影片的原因,因為怕我文章寫的流程或說明不夠完整😂😂😂😂。
留言
張貼留言