微调器不适用于我的Kotlin片段

时间:2019-04-15 08:10:01

标签: android kotlin fragment spinner

我希望出现一个微调框,以使用户在两种类型之间选择,并且不起作用

dt.OrderByDescending(row => row[mycolumn].Length).First();

3 个答案:

答案 0 :(得分:2)

在您的主要活动中,尝试以下操作:

    import android.content.Intent
    import android.os.Bundle
    import android.support.design.widget.Snackbar
    import android.support.v7.app.AppCompatActivity;
    import android.view.View
    import android.widget.ArrayAdapter
    import android.widget.Toast
    import kotlinx.android.synthetic.main.activity_logged.*
    import kotlinx.android.synthetic.main.content_logged.*
    import kotlinx.android.synthetic.main.content_main.*

    class logged : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_logged)
        setSupportActionBar(toolbar)
        // Create an ArrayAdapter
        val adapter = ArrayAdapter.createFromResource(this,
            R.array.city_list, android.R.layout.simple_spinner_item)
        // 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
        }

    fun getValues(view: View) {
        Toast.makeText(this, "Spinner 1 " + spinner.selectedItem.toString()
                , Toast.LENGTH_LONG).show()
    }

    }

我将项目列表放在 strings.xml

<string-array name="city_list">
    <item>Bangkok</item>
    <item>London</item>
    <item>Paris</item>
    <item>Singapore</item>
    <item>New York</item>
    <item>Istanbul</item>
    <item>Dubai</item>
    <item>Kuala Lumpur</item>
    <item>Hong Kong</item>
    <item>Barcelona</item>
</string-array>

答案 1 :(得分:2)

解决方案是在适配器中添加activity?.applicationContext,以将activityFragment更改为上下文:

class Add : Fragment()
{

val types = arrayOf("simple User", "Admin")

override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val t=inflater.inflate(R.layout.fragment_add, container, false)
        val spinner = t.findViewById<Spinner>(R.id.spinner2)
       spinner?.adapter = ArrayAdapter(activity?.applicationContext, R.layout.support_simple_spinner_dropdown_item, types) as SpinnerAdapter
        spinner?.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                println("erreur")
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val type = parent?.getItemAtPosition(position).toString()
                Toast.makeText(activity,type, Toast.LENGTH_LONG).show()
                println(type)
            }

        }
        return t
    }
}

答案 2 :(得分:1)

onViewCreated

spinner2?.adapter = ArrayAdapter(activity, R.layout.support_simple_spinner_dropdown_item, types) as SpinnerAdapter
        spinner2?.onItemSelectedListener = object :AdapterView.OnItemSelectedListener{
            override fun onNothingSelected(parent: AdapterView<*>?) {
                println("erreur")
            }

            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                val type = parent?.getItemAtPosition(position).toString()
                println(type)
            }

        }