Добавляю внутрь override fun onCreateView
этот код:
val array = arrayOf("Option 1", "Option 2", "Option 3")
spinner = view?.findViewById<Spinner>(R.id.spinner)
spinner?.adapter = ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, array)
spinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
// result?.text = array.get(p2)
when(p2) {
// 0 -> result?.text = array.get(0)
// 1 -> result?.text = array.get(1)
// 2 -> result?.text = array.get(2)
}
}
override fun onNothingSelected(p0: AdapterView<*>?) {
// result?.text = array.get(0)
}
}
ArrayAdapter
подчёркивается красным
В MainActivity
всё работало корректно. Подскажите, пожалуйста, как сделать так, чтобы также корректно всё работало и внутри фрагмента.
xml
фрагмента:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".presentation.ExchangeRateFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.appcompat.widget.AppCompatSpinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints" />
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
tools:ignore="MissingConstraints,RedundantNamespace">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sortAlphabetAscending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_weight="1"
android:layout_marginTop = "30dp"
android:padding="20dp"
android:background="@color/black"
android:textColor="@color/white"
android:text="@string/sortASC" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sortAlphabetDescending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_weight="1"
android:layout_marginTop = "30dp"
android:padding="20dp"
android:background="@color/black"
android:textColor="@color/white"
android:text="@string/sortDESC" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sortNumberAscending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_weight="1"
android:layout_marginTop = "30dp"
android:padding="20dp"
android:background="@color/black"
android:textColor="@color/white"
android:text="@string/sortNumberASC" />
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/sortNumberDescending"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_weight="1"
android:layout_marginTop = "30dp"
android:padding="20dp"
android:background="@color/black"
android:textColor="@color/white"
android:text="@string/sortNumberDESC" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/exchangeRateRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginTop="90dp"
app:layout_constraintTop_toBottomOf="@id/spinner"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>
</layout>
метод onCreateView
:
private var binding: FragmentExchangeRateBinding? = null
private var exchangeRateAdapter : ExchangeRateAdapter? = null
private val exchangeRateViewModel : ExchangeRateViewModel? by viewModel()
private val favouriteViewModel: FavouriteViewModel by viewModel()
private var spinner : Spinner? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? {
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_exchange_rate, container, false)
initRecyclerExchangeRate()
loadExchangeRate()
binding?.sortAlphabetAscending?.setOnClickListener {
exchangeRateViewModel?.getSortCurrencyAlphabetAscending?.observe(
viewLifecycleOwner, Observer {
exchangeRateAdapter?.setList(it)
exchangeRateAdapter?.notifyDataSetChanged()
}
)
}
binding?.sortAlphabetDescending?.setOnClickListener {
exchangeRateViewModel?.getSortCurrencyAlphabetDescending?.observe(
viewLifecycleOwner, Observer {
exchangeRateAdapter?.setList(it)
exchangeRateAdapter?.notifyDataSetChanged()
}
)
}
binding?.sortNumberAscending?.setOnClickListener {
exchangeRateViewModel?.getSortCurrencyNumberAscending?.observe(
viewLifecycleOwner, Observer {
exchangeRateAdapter?.setList(it)
exchangeRateAdapter?.notifyDataSetChanged()
}
)
}
binding?.sortNumberDescending?.setOnClickListener {
exchangeRateViewModel?.getSortCurrencyNumberDescending?.observe(
viewLifecycleOwner, Observer {
exchangeRateAdapter?.setList(it)
exchangeRateAdapter?.notifyDataSetChanged()
}
)
}
val array = arrayOf("Option 1", "Option 2", "Option 3")
val rootView = inflater.inflate(R.layout.fragment_exchange_rate, container, false)
spinner = rootView.findViewById<Spinner>(R.id.spinner)
// spinner = view?.findViewById<Spinner>(R.id.spinner)
spinner?.adapter = ArrayAdapter<String>(requireContext(), android.R.layout.simple_list_item_1, array)
// spinner?.adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
/* ArrayAdapter.createFromResource(
activity?.applicationContext!!,
R.array.currency_array,
android.R.layout.simple_spinner_item
).also { adapter ->
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
// Apply the adapter to the spinner
spinner?.adapter = adapter
}*/
spinner?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener{
override fun onItemSelected(p0: AdapterView<*>?, p1: View?, p2: Int, p3: Long) {
// result?.text = array.get(p2)
when(p2) {
// 0 -> result?.text = array.get(0)
// 1 -> result?.text = array.get(1)
// 2 -> result?.text = array.get(2)
}
}
override fun onNothingSelected(p0: AdapterView<*>?) {
// result?.text = array.get(0)
}
}
return binding?.root
}