Android:向列表中添加一个新项目,并使用Kotlin通过RecyclerView适配器显示它
我有一个片段有一个回收视图。当用户单击片段中的按钮(添加新项目)时,应用程序会将用户(通过意图)带到表单中以填写一些数据 当我单击(添加项)时,它应该将数据添加到数组列表并完成该屏幕。返回片段后,应使用适配器将此数据添加到RecyclerView 这是我的代码: 以下是片段:Android:向列表中添加一个新项目,并使用Kotlin通过RecyclerView适配器显示它,android,arraylist,kotlin,adapter,startactivityforresult,Android,Arraylist,Kotlin,Adapter,Startactivityforresult,我有一个片段有一个回收视图。当用户单击片段中的按钮(添加新项目)时,应用程序会将用户(通过意图)带到表单中以填写一些数据 当我单击(添加项)时,它应该将数据添加到数组列表并完成该屏幕。返回片段后,应使用适配器将此数据添加到RecyclerView 这是我的代码: 以下是片段: private val REQUEST_CODE = 2 internal lateinit var adapter: ShipmentItemAdapter override fun onCreateView(inf
private val REQUEST_CODE = 2
internal lateinit var adapter: ShipmentItemAdapter
override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
return inflater!!.inflate(R.layout.fragment_step_three_shipment, container, false)
}
override fun onViewCreated(view: View?, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
btnAddNewShipmentStep3.setOnClickListener {
val intent = Intent(context, AddItemActivity::class.java)
startActivityForResult(intent, REQUEST_CODE)
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data);
val items = ArrayList<ItemsItem>()
if (requestCode == REQUEST_CODE) {
adapter = ShipmentItemAdapter(context, items)
if (resultCode == Activity.RESULT_OK) {
itemsShipments.visibility = View.VISIBLE
itemsShipments.layoutManager = LinearLayoutManager(context)
itemsShipments.setHasFixedSize(true)
itemsShipments.adapter = adapter
} else if (resultCode == Activity.RESULT_CANCELED) {
itemsShipments.visibility = View.GONE
}
}
}
private val请求\u code=2
内部lateinit变量适配器:ShipmentItemAdapter
覆盖创建视图(充气机:布局充气机?、容器:视图组?、savedInstanceState:捆绑?):视图?{
返回充气机!!.充气(R.layout.fragment\u step\u three\u装运,集装箱,错误)
}
覆盖已创建的视图(视图:view?,savedInstanceState:Bundle?){
super.onViewCreated(视图,savedInstanceState)
btnAddNewShipmentStep3.setOnClickListener{
val intent=intent(上下文,AddItemActivity::class.java)
startActivityForResult(意图、请求代码)
}
}
重写activityResult(请求代码:Int,结果代码:Int,数据:Intent?){
super.onActivityResult(请求代码、结果代码、数据);
val items=ArrayList()
if(requestCode==请求\代码){
adapter=ShipmentItemAdapter(上下文,项)
if(resultCode==Activity.RESULT\u确定){
itemsShipments.visibility=View.VISIBLE
itemsShipments.layoutManager=LinearLayoutManager(上下文)
itemsShipments.setHasFixedSize(true)
itemsShipments.adapter=适配器
}else if(resultCode==Activity.RESULT\u已取消){
itemsShipments.visibility=View.GONE
}
}
}
以下是活动:
class AddItemActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_add_item_form)
linPackageAddShipment.setOnClickListener {
edtPackageAddShipment.requestFocus()
}
linPiecesAddShipment.setOnClickListener {
edtPiecesAddShipment.requestFocus()
}
linWeightAddShipment.setOnClickListener {
edtWeightAddShipment.requestFocus()
}
linDescriptionAddShipment.setOnClickListener {
edtDescriptionAddShipment.requestFocus()
}
btnAddItemAddShipment.setOnClickListener {
val errors = collectData()
collectData()
if (errors.isEmpty()) {
val intent = Intent()
setResult(Activity.RESULT_OK, intent)
finish()
} else {
val errorBody = errors.map { getString(it) }.joinToString("\n") { it }
MaterialDialog.Builder(this)
.title("Oops")
.content(errorBody)
.positiveText(R.string.ok)
.positiveColor(ContextCompat.getColor(this, R.color.colorPrimary))
.onAny({ dialog, _ -> dialog.dismiss() })
.show()
}
}
}
private fun collectData(): ArrayList<Int> {
val errors = ArrayList<Int>()
val item = ItemsItem()
if (edtPackageAddShipment.text.toString().isEmpty()) {
errors.add(R.string.packages_must_not_be_empty)
} else {
item.numberOfPackages = edtPackageAddShipment.text.toString().toInt()
}
if (edtPiecesAddShipment.text.toString().isEmpty()) {
errors.add(R.string.pieces_must_not_be_empty)
} else {
item.numberOfPieces = edtPiecesAddShipment.text.toString()
}
if (edtWeightAddShipment.text.toString().isEmpty()) {
errors.add(R.string.weight_must_not_be_empty)
} else {
item.weightUnit = edtWeightAddShipment.text.toString().toInt()
}
if (edtHeightAddShipment.text.toString().isEmpty()) {
errors.add(R.string.height_must_not_be_empty)
} else {
item.height = edtHeightAddShipment.text.toString()
}
if (edtWidthAddShipment.text.toString().isEmpty()) {
errors.add(R.string.width_must_not_be_empty)
} else {
item.width = edtWidthAddShipment.text.toString()
}
if (edtLengthAddShipment.text.toString().isEmpty()) {
errors.add(R.string.length_must_not_be_empty)
} else {
item.length = edtLengthAddShipment.text.toString()
}
if (edtDescriptionAddShipment.text.toString().isEmpty()) {
errors.add(R.string.description_must_not_be_empty)
} else {
item.description = edtDescriptionAddShipment.text.toString()
}
CreateNewShipmentActivity.mainShipment.shipmentObj.items?.add(item)
return errors
}
override fun onBackPressed() {
setResult(Activity.RESULT_CANCELED)
super.onBackPressed()
}
}
class AddItemActivity:AppCompatActivity(){
重写创建时的乐趣(savedInstanceState:Bundle?){
super.onCreate(savedInstanceState)
setContentView(R.layout.activity\u add\u item\u表单)
linPackageAddShipment.setOnClickListener{
edtpackeaddshipment.requestFocus()文件
}
LinpieceAddShipping.setOnClickListener{
edtpagesaddshipping.requestFocus()
}
LinweightAddShipping.setOnClickListener{
EDTweightAddShipping.requestFocus()
}
LindescriptionAddShipping.setOnClickListener{
edtDescriptionAddShipment.requestFocus()文件
}
btnAddItemAddShipment.setOnClickListener{
val errors=collectData()
收集数据()
if(errors.isEmpty()){
val intent=intent()
setResult(Activity.RESULT_OK,intent)
完成()
}否则{
val errorBody=errors.map{getString(it)}.joinToString(“\n”){it}
MaterialDialog.Builder(此)
.标题(“Oops”)
.内容(错误正文)
.positiveText(R.string.ok)
.positiveColor(ContextCompat.getColor(this,R.color.colorPrimary))
.onAny({dialog,{->dialog.disease()})
.show()
}
}
}
private fun collectData():ArrayList{
val errors=ArrayList()
val item=ItemsItem()
如果(edtPackageAddShipment.text.toString().isEmpty()){
errors.add(R.string.packages\u不能为空)
}否则{
item.numberOfPackages=edtPackageAddShipment.text.toString().toInt()
}
如果(EDTPieseddShipping.text.toString().isEmpty()){
错误。添加(R.string.pieces\u不能为空)
}否则{
item.numberOfPieces=EDTPieseAndShipping.text.toString()
}
如果(EDTweightAddShipping.text.toString().isEmpty()){
错误。添加(R.string.weight\u不能为空)
}否则{
item.weightUnit=EDTweightAddShipping.text.toString().toInt()
}
如果(edTheightAddShipping.text.toString().isEmpty()){
错误。添加(R.string.height\u不能为空)
}否则{
item.height=edtheightaddshipping.text.toString()
}
如果(edtwidthAddShipping.text.toString().isEmpty()){
错误。添加(R.string.width\u不能为空)
}否则{
item.width=EDTwidthAddShipping.text.toString()
}
如果(edtLengthAdShipment.text.toString().isEmpty()){
错误。添加(R.string.length\u不能为空)
}否则{
item.length=edtlenghthaddshipment.text.toString()
}
如果(edtDescriptionAddShipment.text.toString().isEmpty()){
错误。添加(R.string.description\u不能为空)
}否则{
item.description=edtDescriptionAddShipment.text.toString()
}
CreateNewShipmentActivity.MainShipping.shipmentObj.items?添加(项目)
返回错误
}
重写函数onBackPressed(){
setResult(活动结果\u已取消)
super.onBackPressed()
}
}
下面是适配器:
class ShipmentItemAdapter internal constructor(private val context: Context, private val items: List<ItemsItem>) : RecyclerView.Adapter<ShipmentItemAdapter.MyViewHolder>() {
private val inflater: LayoutInflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
val view = inflater.inflate(R.layout.item_shipment_package, parent, false)
return MyViewHolder(view).listen { pos, type ->
val item = items[pos]
val intent = Intent(context, AddItemActivity::class.java)
startActivity(context, intent, null)
}
}
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
holder.namePackageShipmentPackage.text = items[position].description
holder.quantityPackageShipmentPackage.text = items[position].description
holder.dimensionPackageHeightShipmentPackage.text = items[position].description
holder.dimensionPackageWidthShipmentPackage.text = items[position].description
holder.dimensionPackageLengthShipmentPackage.text = items[position].description
}
override fun getItemCount(): Int {
return items.size
}
inner class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
var namePackageShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.namePackageShipmentPackage)
var quantityPackageShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.quantityPackageShipmentPackage)
var dimensionPackageHeightShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.dimensionPackageHeightShipmentPackage)
var dimensionPackageWidthShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.dimensionPackageWidthShipmentPackage)
var dimensionPackageLengthShipmentPackage: AppCompatTextView = itemView.findViewById(R.id.dimensionPackageLengthShipmentPackage)
}
private fun <T : RecyclerView.ViewHolder> T.listen(event: (position: Int, type: Int) -> Unit): T {
itemView.setOnClickListener {
event.invoke(adapterPosition, itemViewType)
}
return this
}
class ShipmentItemAdapter内部构造函数(private-val-context:context,private-val-items:List):RecyclerView.Adapter(){
私有val充气器:LayoutInflater=LayoutInflater.from(上下文)
重写CreateViewHolder(父级:ViewGroup,viewType:Int):MyViewHolder{
val视图=充气机。充气(R.layout.item_Shipping_package,父项,false)
返回MyViewHolder(视图)。侦听{pos,键入->
val项目=项目[pos]
val intent=intent(上下文,AddItemActivity::class.java)
startActivity(上下文、意图、空)
}
}
覆盖onBindViewHolder(holder:MyViewHolder,位置:Int){
holder.namePackageShipmentPackage.text=项目[位置]。说明
holder.quantityPackageShipmentPackage.text=项目[位置]。说明
holder.dimensionPackageHeightShipmentPackage.text=项目[位置]。说明
holder.dimensionPackageWidthShipmentPackage.text=项目[位置]。说明
holder.dimensionPackageLengthShipmentPackage.text=项目[位置]。说明
}
重写getItemCount():Int{
返回项目。大小
}
内部类MyViewHolder(itemView:View):RecyclerView.ViewHolder(ite)
class ShipmentObj {
var items: ArrayList<ItemsItem?>? = null
}
data class ItemsItem(
var numberOfPieces: String? = null,
var length: String? = null,
var description: String? = null,
var numberOfPackages: Int? = null,
var number: Int? = null,
var width: String? = null,
var height: String? = null,
var weightUnit: Int? = null
)
val items = ArrayList<ItemsItem>()
adapter = ShipmentItemAdapter(context, items)
if (errors.isEmpty()) {
val intent = Intent()
setResult(Activity.RESULT_OK, intent)
finish()
}