android-studiokotlinfragmentdialogfragment

Kotlin - String from Edittext in DialogFragment don't save in data class


I have one Edittext in DialogFragment and that create a new group. When I press "OK" Group is created but without string.

I try figure out but complitly I don't know where is mistake.

my DialogFragment:

class NewCategory(val listener: NewCategoryCreateListener) : DialogFragment() {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val view = requireActivity()
        val newCategoryView = view.layoutInflater.inflate(R.layout.new_category, null)
        val txtNewCat = newCategoryView.findViewById<EditText>(R.id.etNewCategory)
        val newCat = txtNewCat.text.toString()

        return AlertDialog.Builder(requireContext())
            .setView(newCategoryView)
            .setTitle("Create new category")
            .setPositiveButton("OK") {di, i ->
                listener.newCategoryCreate(newCat)
            }
            .setNegativeButton("Cancel") { di, i -> }
            .create()
    }

    interface NewCategoryCreateListener{
        fun newCategoryCreate(text: String)
    }
}

my MainFragment:

class MainFragment : Fragment(), NewCategory.NewCategoryCreateListener {

    private var _binding: FragmentMainBinding? = null
    private val binding
        get() = _binding!!

    private lateinit var shoppingListViewModel: ShoppingListViewModel
    private lateinit var categoryAdapter: CategoryAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        binding.categoryRV.layoutManager = LinearLayoutManager(requireContext())
        shoppingListViewModel.allCategories.observe(viewLifecycleOwner, {
            updateCategories(it)
        })

        binding.addCategory.setOnClickListener {
            val newCategory = NewCategory(this)
            newCategory.show(childFragmentManager, "NewCategory")
        }
    }


    private fun updateCategories(list: List<Category>) {
        categoryAdapter = CategoryAdapter(list)
        binding.categoryRV.adapter = categoryAdapter
    }

    override fun newCategoryCreate(text: String) {
        val newCat = Category(text)
        shoppingListViewModel.insertCategory(newCat)
    }

}

What I doing wrong?


Solution

  • You are setting your newCat string to the text view text when the dialog is created, not when the okay button is hit. So your string is empty.

    Try moving val newCat = txtNewCat.text.toString() to inside your okay button callback like this:

    
            return AlertDialog.Builder(requireContext())
                .setView(newCategoryView)
                .setTitle("Create new category")
                .setPositiveButton("OK") {di, i ->
                    val newCat = txtNewCat.text.toString()
                    listener.newCategoryCreate(newCat)
                }
                .setNegativeButton("Cancel") { di, i -> }
                .create()