我在里面添加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
}
构造函数参数之一
ArrayAdapter
是Context
:你在fragment中只能得到
this
不满足构造函数要求的fragment本身,试试这个:也就是说,我们将它作为 context 传递
requireContext()
,或者你可以简单地尝试,context
但我认为你会被提议用一个新的来修复它requireContext()
。你也可以用this.requireContext()
.UPD
尝试在适配器声明后添加:
要初始化小部件,您需要执行以下操作: