用RecyclerView顯示資料
<TextView
android:text="TextView"
android:textColor="#000000" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tv_name" android:layout_weight="1"/>
<TextView
android:text="TextView"
android:textColor="#000000" android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/tv_ways" android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" app:srcCompat="@android:drawable/ic_menu_edit"
android:id="@+id/imageView" android:layout_weight="1"/>
因為文字非常多,所以在RecyclerView的Layout中只放入兩個TextView,和一個ImageView當作修改的按鈕
建立資料庫物件和Adapter物件及一個ArrayList當作資料來源
private val item = ArrayList<Notes>()
private lateinit var db : SQLiteDatabase
private lateinit var adapter : MyAdapter
在Adapter中,對三個元件監聽,當兩個TextView被按下時,彈出一個對話框顯示詳細的內容
class MyAdapter (private val context: Context,private val data: ArrayList<Notes>): RecyclerView.Adapter<MyAdapter.ViewHolder>() {
class ViewHolder(v: View): RecyclerView.ViewHolder(v) {
val tv_name = v.findViewById<TextView>(R.id.tv_name)
val tv_ways = v.findViewById<TextView>(R.id.tv_ways)
}
override fun onCreateViewHolder(viewGroup: ViewGroup, position: Int): ViewHolder {
val v = LayoutInflater.from(viewGroup.context).inflate(R.layout.item,viewGroup,false)
return ViewHolder(v)
}
override fun getItemCount() = data.size
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
var dialog = AlertDialog.Builder(context)
.setTitle(data[position].name)
.setMessage("沖煮方式:${data[position].ways}\n" +
"酸:${data[position].acidity} 苦:${data[position].bitter} 甜:${data[position].sweet}\n" +
"風味描述:\n" + "${data[position].flavor}")
.setPositiveButton("確認"){dialog,i->}
holder.tv_name.text = data[position].name
holder.tv_ways.text = data[position].ways
holder.tv_name.setOnClickListener {
dialog.show()
}
holder.tv_ways.setOnClickListener {
dialog.show()
}
}
}
在MainActivity中,利用SQL指令取得資料庫的資料
fun viewUpdate() {
db = DBHelper(this).writableDatabase
//從資料庫取得資料
val notes = db.rawQuery("SELECT * FROM Notes", null)
//移動到第一筆資料
notes.moveToFirst()
item.clear()
for (i in 0 until notes.count) {
item.add(
Notes(
notes.getLong(0), notes.getString(1), notes.getInt(2), notes.getInt(3),
notes.getInt(4), notes.getString(5), notes.getString(6))
)
//移動到下一筆資料
notes.moveToNext()
//更新RecyclerView
adapter.notifyDataSetChanged()
}
}
startActivityForResult()
使用startActivityForResult()
,可以讓被啟動的Activity回傳資料到onActivityResult()
,啟動時設定requestCode判斷被啟動的是哪個Activity,在被啟動的Activity設定resultCode判斷資料處理情形
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
btn_add.setOnClickListener {
val i = Intent(this,EditActivity::class.java)
startActivityForResult(i,1)//將requestCode設為1 啟動EditActivity
}
}
//當EditActivity結束時啟動
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
data?.extras.let {
//當EditActivity有存資料到資料庫時執行
if(requestCode == 1 && resultCode == Activity.RESULT_OK){
viewUpdate()
}
}
}
在EditActivity加入setResult(Activity.RESULT_OK)
設置ResultCode
btn_add.setOnClickListener {
val db = DBHelper(this).writableDatabase
val Id = System.currentTimeMillis()
//儲存資料
db.execSQL("INSERT INTO Notes(id,name,acidity,bitter,sweet,ways,flavor) VALUES(?,?,?,?,?,?,?)",
arrayOf<Any?>(Id,"${ed_name.text}",seekBar_acidity.progress,seekBar_bitter.progress,seekBar_sweet.progress,"${ed_name.text}","${ed_flavor.text}")
)
//將ResultCode設為 RESULT_OK
setResult(Activity.RESULT_OK)
finish()
}