[英]How to add data step by step in android?
這可以通過創建一個列表來完成,當您點擊 +Add other deposit 按鈕時,您可以使用 recyclerview 適配器將一個新項目添加到列表中。 輸入字段的一個問題,我在用戶按下鍵盤上的完成按鈕時保存信息,也可以在失去焦點時保存字段中的數據,決定按下后保存更好。
package com.myply.myapplication
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.google.android.material.floatingactionbutton.FloatingActionButton
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val btnAddOtherDeposit = findViewById<TextView>(R.id.btn_add_other_deposit)
val rvCustomers = findViewById<RecyclerView>(R.id.rv_customers)
val fabDone = findViewById<FloatingActionButton>(R.id.fab_done)
val adapter = MyRecyclerAdapter(this, arrayListOf(CustomerModel()))
rvCustomers.adapter = adapter
rvCustomers.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)
adapter.mClickListener = object : MyRecyclerAdapter.ItemClickListener {
override fun onItemRemoveClicked(position: Int) {
adapter.removeAt(position)
}
}
btnAddOtherDeposit.setOnClickListener {
/*add empty model without information */
adapter.add(CustomerModel())
}
fabDone.setOnClickListener {
/*collect all data*/
var customers = adapter.data
}
}
}
適配器
package com.myply.myapplication
import android.content.Context
import android.view.KeyEvent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import android.widget.RadioButton
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
class MyRecyclerAdapter internal constructor(
val context: Context?,
val data: MutableList<CustomerModel>
) : RecyclerView.Adapter<MyRecyclerAdapter.ViewHolder>() {
var mClickListener: ItemClickListener? = null
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
val view = LayoutInflater.from(context).inflate(R.layout.item_customer, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val customer = data[position]
holder.editName.setText(customer.name)
holder.rbDeposit.setOnCheckedChangeListener(null)
holder.rbCheque.setOnCheckedChangeListener(null)
holder.rbDeposit.isChecked = customer.depositType == "Deposit"
holder.rbCheque.isChecked = customer.depositType == "Cheque"
holder.btnRemove.setOnClickListener { mClickListener?.onItemRemoveClicked(position) }
holder.editName.setOnEditorActionListener(object : TextView.OnEditorActionListener {
override fun onEditorAction(p0: TextView?, actionId: Int, event: KeyEvent?): Boolean {
if (actionId == EditorInfo.IME_ACTION_SEARCH || actionId == EditorInfo.IME_ACTION_DONE || event?.action == KeyEvent.ACTION_DOWN && event?.keyCode == KeyEvent.KEYCODE_ENTER) {
customer.name = holder.editName.text.toString()
holder.editName.clearFocus()
update(customer, position)
return true
}
return false
}
})
holder.rbDeposit.setOnCheckedChangeListener { compoundButton, b ->
customer.depositType = "Deposit"
update(customer, position)
}
holder.rbCheque.setOnCheckedChangeListener { compoundButton, b ->
customer.depositType = "Cheque"
update(customer, position)
}
}
override fun getItemCount(): Int {
return data.size
}
fun add(customer: CustomerModel) {
data.add(customer)
notifyItemInserted(data.size - 1)
}
fun update(customer: CustomerModel, position: Int) {
data[position] = customer
notifyItemChanged(position)
}
fun removeAt(position: Int) {
data.removeAt(position)
notifyItemRemoved(position)
notifyItemRangeChanged(position, data.size)
}
inner class ViewHolder internal constructor(itemView: View) :
RecyclerView.ViewHolder(itemView) {
var editName: EditText = itemView.findViewById(R.id.edit_name)
var btnRemove: ImageView = itemView.findViewById(R.id.btn_remove)
var rbDeposit: RadioButton = itemView.findViewById(R.id.rb_deposit)
var rbCheque: RadioButton = itemView.findViewById(R.id.rb_cheque)
}
interface ItemClickListener {
fun onItemRemoveClicked(position: Int)
}
}
item_customer.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<androidx.appcompat.widget.AppCompatEditText
android:id="@+id/edit_name"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:singleLine="true" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RadioButton
android:id="@+id/rb_deposit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Deposit type" />
<RadioButton
android:id="@+id/rb_cheque"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cheque" />
</LinearLayout>
</LinearLayout>
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/btn_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
app:srcCompat="@android:drawable/ic_menu_close_clear_cancel" />
</RelativeLayout>
活動_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".MainActivity">
<androidx.core.widget.NestedScrollView 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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_customers"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/btn_add_other_deposit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="Add other deposit" />
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab_done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_margin="16dp" />
</RelativeLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.