简体   繁体   中英

Passing a value from a dropdown list in a fragment to another activity (Kotlin)

I am making an app (ehealth portal) in Android Studio using Kotlin language and in that app the user should be able to book an appointment choosing date/time/doctor's name from a dropdown list and once they push the "book a date" button a confirmation screen appears that has the values of the dropdown lists (date/time/doctor's name) passed into a confirmation text confirmation activity instance booking a date interface .

CalendarFragment.kt

package com.example.mydoctor

import...


class CalendarFragment : Fragment() {

    private lateinit var binding: FragmentCalendarBinding1

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setHasOptionsMenu(true)
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        super.onCreate(savedInstanceState)
        binding = FragmentCalendarBinding1.inflate(layoutInflater)

        val itemsDates = resources.getStringArray(R.array.dates)
        val adapterDate = ArrayAdapter(requireContext(), list_dates, itemsDates)
        binding.autocompleteTextViewDateDropdown.setAdapter(adapterDate)

        val itemsTimes = resources.getStringArray(R.array.times)
        val adapterTime = ArrayAdapter(requireContext(), list_times, itemsTimes)
        binding.autocompleteTextViewTimeDropdown.setAdapter(adapterTime)

        binding.bookADateButton.setOnClickListener {
            val intent = Intent(requireContext(),ConfirmationActivity::class.java)
            intent.putExtra("Confirmation","Confirmation Value")
            startActivity(intent)
        }

        return binding.root

    }

}

ConfirmationActivity.kt

package com.example.mydoctor

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log

class ConfirmationActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_confirmation)

        val value = intent.getStringExtra("Confirmation").toString()
        Log.d("conf","Values is: ${value}")
    }
}

How can I go about that? I know I have to use getExtra() or getStringExtra() but how should I actually get the value from the calendar fragment?

EDIT:

I tried changing the setOnClickListener in the CalendarFragment.kt as follows but it does not seem to work storing the value:

 binding.bookADateButton.setOnClickListener {

            (date_dropdown.getEditText() as AutoCompleteTextView).onItemClickListener =
            OnItemClickListener {
                    adapterView, view, position, _ ->
                    val selectedValue: String? = adapterDate.getItem(position)
                
                }

            val intent = Intent(requireContext(),ConfirmationActivity::class.java)
            intent.putExtra("Confirmation","Confirmation Value")
            startActivity(intent)
        }

You have to setup your date_dropdown listener outside of the setOnClickListener, otherwise you only start listening to date_dropdown changes after the first click is made, which is already too late.

So change the order in your code

// have a variable to track the last selected value with a larger
// scope somewhere inside your Fragment
var selectedValue: String? = null

(date_dropdown.getEditText() as AutoCompleteTextView).onItemClickListener =
  OnItemClickListener { adapterView, view, position, _ ->
    // this code block is called every time an item is clicked
    selectedValue = adapterDate.getItem(position)
}

binding.bookADateButton.setOnClickListener {
    // this code block is called every time the 'bookADateButton' is clicked       
    val intent = Intent(requireContext(), ConfirmationActivity::class.java)
    intent.putExtra("Confirmation", selectedValue ?: "No value was selected.")
    startActivity(intent)
}

There is also the setOnItemSelectedListener available, if you don't want to rely on clicks/taps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM