android 如何通过Kotlin在片段中使用ArrayAdapter

esbemjvw  于 2023-01-24  发布在  Android
关注(0)|答案(2)|浏览(154)

我试图在Fragment中创建Spinner,但是在ArrayAdapter构造函数调用中出现错误。我不知道为什么,但是它有一个红色下划线。除此之外,没有错误。当我在Activity中使用相同的ArrayAdapter时,它可以工作,但是在Fragment中,它给出了一个错误。
我的FirstFragment.kt

class FirstFragment : Fragment() {

    private var mListener: OnFragmentInteractionListener? = null

    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        // Inflate the layout for this fragment
        return inflater!!.inflate(R.layout.fragment_first, container, false)
    }

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

        /*Find the id of spinner*/
        val spinner = lol

        /*set an adapter with strings array*/
        spinner.adapter = ArrayAdapter(this, R.layout.support_simple_spinner_dropdown_item, resources.getStringArray(R.array.atoms)) as SpinnerAdapter?

            /*set click listener*/
            spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(parent: AdapterView<*>, view: View, position: Int, id: Long) {
                val num = when (spinner.selectedItem.toString()) {
                    "H" -> editText.setText("1")
                    "He" -> editText.setText("4")
                    "C" -> editText.setText("12")
                    "O" -> editText.setText("16")
                    else -> editText.setText("")
                }
            }

            override fun onNothingSelected(parent: AdapterView<*>) {
                /*Do something if nothing selected*/
            }
        }

        button.setOnClickListener {
            if (
                editText2.text.toString().length > 0 &&
                editText.text.toString().length > 0) {
                val num2 = editText.text.toString().toDouble()
                val num1 = editText2.text.toString().toDouble()
                val num = num1/num2
                textView.setText("$num moles")
            }
            else {
                textView.setText("Please Enter a correct value")
            }
        }
    }
}

我的fragment_first.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.a3.aakap.ftrial.FirstFragment">

    <android.support.constraint.ConstraintLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="294dp"
            android:layout_height="80dp"
            android:layout_weight="1"
            android:text="Amswer : No of Moles"
            android:textAlignment="center"
            android:textSize="20sp"
            app:layout_anchorGravity="right|top"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.556" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="143dp"
            android:layout_height="46dp"
            android:layout_weight="1"
            android:text="Amount in Grams "
            android:textAlignment="center"
            android:textSize="20sp"
            app:layout_anchorGravity="left|bottom"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.066"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.305" />

        <Spinner
            android:id="@+id/lol"
            android:layout_width="145dp"
            android:layout_height="57dp"
            app:layout_anchorGravity="left|top"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.07"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.136" />

        <Button
            android:id="@+id/button"
            android:layout_width="138dp"
            android:layout_height="43dp"
            android:text="Calculate"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.788" />

        <EditText
            android:id="@+id/editText"
            android:layout_width="148dp"
            android:layout_height="57dp"
            android:ems="10"
            android:hint="Gram"
            android:inputType="numberDecimal|number"
            app:layout_anchorGravity="bottom|center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.762"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.137" />

        <EditText
            android:id="@+id/editText2"
            android:layout_width="148dp"
            android:layout_height="57dp"
            android:ems="10"
            android:hint="Gram"
            android:inputType="numberDecimal|number"
            app:layout_anchorGravity="center_horizontal|center"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.716"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.288" />

    </android.support.constraint.ConstraintLayout>

</FrameLayout>
pjngdqdw

pjngdqdw1#

问题出在此构造函数调用中:

spinner.adapter = ArrayAdapter(
    this,
    R.layout.support_simple_spinner_dropdown_item,
    resources.getStringArray(R.array.atoms)
) as SpinnerAdapter

参数this必须是对Context的引用,并且Fragment不是Context。在Activity中,该参数有效,因为ActivityContext
解决方案是将this替换为activity

spinner.adapter = ArrayAdapter(
    activity,
    R.layout.support_simple_spinner_dropdown_item,
    resources.getStringArray(R.array.atoms)
)
rekjcdws

rekjcdws2#

我来这里寻找一些答案,但我发现我的解决方案更好,更短,我猜和完成任务,我只是把它放在这里的情况下

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

    getCates()

}

该函数

private fun getCates() {
    val categories = MyApplication().getCategories()

    val adapter = ArrayAdapter(requireContext(), R.layout.list_item, categories)

    (binding.autocomplete as? AutoCompleteTextView)?.setAdapter(adapter)

    binding.autocomplete.setOnItemClickListener { _, _, i, _ ->
        category = categories[i]
        Log.d(TAG,"$category")
    }

}

附言:我得到的清单项目从firebase你可以添加项目lovaly

相关问题