Home > OS >  add data to a listView from another activity kotlin
add data to a listView from another activity kotlin

Time:12-01

I am new to this area, I am having problems with the following example.

enter image description here

I want to add the product data when the ImageButton is clicked to my Product Activity, to the other order detail activity.

this is my my eclient class

data class epedidos (

var cod_producto: String,
var nom_producto: String,
var cantidad_producto : Int,
var total_apagar : Float


    ){}

This is my ListAdapter.kt adapter

class ListAdapter (var mCtx: Context, var resource:Int, var  items:ArrayList<epedidos>)
: ArrayAdapter<epedidos>(mCtx,resource,items) {

override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {

    val layoutInflater: LayoutInflater = LayoutInflater.from(mCtx)

    val view: View = layoutInflater.inflate(resource, null)

   // val imageView: ImageView = view.findViewById(R.id.img_productos)
    val textpenomproduct: TextView = view.findViewById(R.id.txtpe_nom_product)
    val textpecodproduc: TextView = view.findViewById(R.id.txtpe_cod_produc)
    val textpe_monto: TextView = view.findViewById(R.id.txtpe_Monto)
    val textpecantidad: TextView = view.findViewById(R.id.txtpe_cantidad)

    var myItems: epedidos = items[position]

    textpecodproduc.text = myItems.cod_producto
    textpenomproduct.text = myItems.nom_producto
    textpecantidad.text = myItems.cantidad_producto.toString()
    textpe_monto.text = myItems.total_apagar.toString()
    return view
}}

From my activity product_detail.kt in my event

override fun onClick(p0: View?) {

    when (p0?.id) {

        R.id.imgNuevoPedido -> {

            val intent = Intent(this@DetalleProducto, DetallePedido::class.java)

            val var_acodproducto = findViewById<TextView>(R.id.txtdetcodproducto)
            val var_detanomproducto = findViewById<TextView>(R.id.txtdetnomproducto)
            val var_detamarcaproducto = findViewById<TextView>(R.id.txtdetmarca)
            val var_detalineaproducto = findViewById<TextView>(R.id.txtdetlinea)
            val var_detaprecioproducto = findViewById<TextView>(R.id.txtdetprecioproduct)


            val cod_producto = intent.getStringExtra("codproducto")
            val nom_producto = intent.getStringExtra("nombreproducto")
            val marca_producto = intent.getStringExtra("marca")
            val linea_producto = intent.getStringExtra("linea")
            val precio_producto = intent.getStringExtra("precio")
            var_acodproducto.setText(cod_producto)
            var_detanomproducto.setText(nom_producto)
            var_detamarcaproducto.setText(marca_producto)
            var_detalineaproducto.setText(linea_producto)
            var_detaprecioproducto.setText(precio_producto)


            startActivity(intent)

        }


    }
}

But I have no idea how to get the data that is sent from the product_detail activity and that is added to my list in my Order_Detail activity

Any idea or reference.

Thanks.

CodePudding user response:

If I understand correctly, you want to pass the data from DetalleProducto activity to DetallePedido activity right?

What you can do is pass the information as Intent Extras, embedded into the intent you use to start DetallePedido.

It should look pretty much like what you are already doing, but instead of using getStringExtra try intent.putExtra(...).

You should use the getExtra functions in the receiving activity as follows:

override fun onCreate(savedInstanceState: Bundle?) {
    // ...
    val codProd: String? = intent.getStringExtra("codproducto")
    // ...

Furthermore, you can make your epedido class Parcelable and you can just toss it directly into the extras and receive it on the other side.

Check this link for more details: https://developer.android.com/guide/components/activities/parcelables-and-bundles

  • Related