Home > OS >  How to pass data from an AppCompatDialog to an AppCompatActivity
How to pass data from an AppCompatDialog to an AppCompatActivity

Time:10-22

i don't know how to pass data from the dialog fragment to the Activity. I have an Activity, which creates the Dialog. From this Dialog i want to pass Data to another Activity. Anyone know i can do this? Thanks :)

CodePudding user response:

this is my 1st Activity:

class EinkaufslisteActivity : AppCompatActivity() {
//override val kodein by kodein()
//private val factory : EinkaufsViewModelFactory by instance()

@SuppressLint("NotifyDataSetChanged")
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_einkaufsliste)

    val database = EinkaufDatenbank(this)
    val repository = EinkaufsRepository(database)
    val factory = EinkaufsViewModelFactory(repository)


    val viewModel = ViewModelProviders.of(this, factory).get(EinkaufsViewModel::class.java)

    val adapter = ProduktAdapter(listOf(), viewModel)

    rvVorratsliste.layoutManager = LinearLayoutManager(this)
    rvVorratsliste.adapter = adapter

    viewModel.getAllProdukte().observe(this, Observer {
        adapter.items = it
        adapter.notifyDataSetChanged()
    })



    adapter.setOnItemClickListener {
        val produkt = it
        Intent(this, VorratslisteActivity::class.java).also {
            it.putExtra("EXTRA_PRODUKT", produkt)
        }
        EinkaufslisteProduktGekauftDialog(this, produkt, object : AddDialogListener{
            override fun onAddButtonClicked(produkt: Produkt) {

            }
            override fun onAddButtonClickedVorrat(produktVorrat: ProduktVorrat) {
                viewModel.delete(produkt)
                
            }
        }).show()
    }

This is my Dialog:

ass EinkaufslisteProduktGekauftDialog (context: Context, var produkt : Produkt?, var addDialogListener: AddDialogListener?) : AppCompatDialog(context){


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.dialog_einkaufsliste_produkt_gekauft)


    tvProduktgekauftName.text = produkt?.name.toString()
    etProduktGekauftAnzahl.hint = produkt?.anzahl.toString()


    btnProduktGekauftOk.setOnClickListener {
        val name = tvProduktgekauftName.text.toString()
        val anzahl = etProduktGekauftPreis.text.toString()
        val datum = etProduktGekauftDatum.text.toString()
        val preis = etProduktGekauftPreis.text.toString()

        if(name.isEmpty() || anzahl.isEmpty()){
           Toast.makeText(context, "Bitte fülle alle Felder aus", Toast.LENGTH_SHORT).show()
            return@setOnClickListener
        }


        val produktVorrat = ProduktVorrat(name, anzahl.toInt(), datum)
        addDialogListener?.onAddButtonClickedVorrat(produktVorrat)
        dismiss()
    }

This is my 2nd Activity:

class VorratslisteActivity : AppCompatActivity(){



override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_vorratsliste)



    val database = EinkaufDatenbank(this)
    val repository = VorratsRepository(database)
    val factory = VorratViewModelFactory(repository)


    val viewModel = ViewModelProviders.of(this, factory).get(VorratViewModel::class.java)

    val adapter = ProduktVorratAdapter(listOf(), viewModel)


    rvVorratsliste.layoutManager = LinearLayoutManager(this)
    rvVorratsliste.adapter = adapter

    viewModel.getAllProdukteVorratsliste().observe(this, Observer {
        adapter.items = it
        adapter.notifyDataSetChanged()
    })

    val produkt = intent.getSerializableExtra("EXTRA_PRODUKT") as? ProduktVorrat
    if(produkt != null) {
        viewModel.upsertVorrat(produkt)
    }

    btnVorratNeuesProdukt.setOnClickListener {
        VorratProduktHinzufuegenDialog(this,
            object : AddDialogListener {
                override fun onAddButtonClicked(produkt: Produkt) {
                    TODO("Not yet implemented")
                }

                override fun onAddButtonClickedVorrat(produktVorrat: ProduktVorrat) {
                viewModel.upsertVorrat(produktVorrat)
            }
        }).show()
    }

The "produkt" in activity 2 is null and i don't know why

CodePudding user response:

ActivityA launches Dialog

Dialog passes result back to ActivityA

ActivityA launches ActivityB passing result from Dialog

  • Related