gpt4 book ai didi

java - 如何获取用户在相关微调器上选择的数据?

转载 作者:行者123 更新时间:2023-12-02 08:55:08 25 4
gpt4 key购买 nike

我想做什么

我想获取用户在相关微调器上选择的数据。

在我当前的代码中,成功从第一个旋转器获取数据,但无法从第二个旋转器获取数据。

问题

我应该如何修复我的代码以获取用户选择的适当数据?

  1. 在本例中,第二个 textView 应该是 BMW,但它是 Audi,它是第二个旋转器的第一个候选者。

motor

  • 第一个 textView 根据用户的选择而更改。在本例中,它从电机更改为移动,但同样,第二个 textView 仍保留为第二个旋转器的第一个候选。
  • mobile

    当前代码

    MainActivity.kt

    package com.example.spinner

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.Menu
    import android.view.MenuItem
    import android.app.Activity
    import android.content.Intent
    import android.util.Log
    import android.view.View
    import android.widget.AdapterView
    import android.widget.Spinner
    import android.widget.ArrayAdapter
    import kotlinx.android.synthetic.main.activity_main.*
    import android.widget.AdapterView.OnItemSelectedListener
    import android.widget.Toast

    class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {

    var spinner1: Spinner? = null
    var spinner2: Spinner? = null
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    spinner1 = findViewById(R.id.spinner1) as Spinner
    spinner2 = findViewById(R.id.spinner2) as Spinner
    val adapter1 = ArrayAdapter.createFromResource(
    this,
    R.array.array1, android.R.layout.simple_spinner_item
    )
    spinner1?.setAdapter(adapter1)
    spinner1?.setOnItemSelectedListener(this)
    }
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the main; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.main, menu)
    return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    val id = item.getItemId()
    return if (id == R.id.action_settings) {
    true
    } else super.onOptionsItemSelected(item)
    }

    override fun onItemSelected(
    parent: AdapterView<*>, view: View, position: Int,
    id: Long
    ) {
    if (spinner1?.getSelectedItem() == "mobile") {
    Toast.makeText(
    applicationContext, "Mobil dipilih",
    Toast.LENGTH_SHORT
    ).show()

    val adapter2 = ArrayAdapter.createFromResource(
    this,
    R.array.mobile_array, android.R.layout.simple_spinner_item
    )
    spinner2?.setAdapter(adapter2)
    spinner2?.setOnItemSelectedListener(this)

    } else {
    val adapter2 = ArrayAdapter.createFromResource(
    this,
    R.array.motor_array, android.R.layout.simple_spinner_item
    )
    spinner2?.setAdapter(adapter2)
    spinner2?.setOnItemSelectedListener(this)

    }


    // here I try to put the selected contents to variables
    val spinner1_content = spinner1?.getSelectedItem() as String
    textView1.text = spinner1_content
    val spinner2_content = spinner2?.getSelectedItem() as String
    textView2.text = spinner2_content


    }


    override fun onNothingSelected(parent: AdapterView<*>?) {

    }
    }

    activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.stackspinner.MainActivity" >

    <Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

    <Spinner
    android:id="@+id/spinner2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/spinner1" />


    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical">

    <TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#00f"
    android:layout_marginTop="100dp"
    android:textSize="40sp" />

    <TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#00f"
    android:layout_marginTop="100dp"
    android:textSize="40sp" />

    </LinearLayout>

    </RelativeLayout>

    字符串.xml

    <resources>

    <string name="app_name">Application Name</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>

    <string-array name="array1">
    <item>mobile</item>
    <item>motor</item>
    </string-array>
    <string-array name="mobile_array">
    <item>Android</item>
    <item>blackberry</item>
    <item>apple</item>
    </string-array>
    <string-array name="motor_array">
    <item>Audi</item>
    <item>BMW</item>
    <item>unicorn</item>
    </string-array>
    </resources>

    听到答案后的当前代码

    MainActivity.kt

    package com.example.spinner

    import androidx.appcompat.app.AppCompatActivity
    import android.os.Bundle
    import android.view.Menu
    import android.view.MenuItem
    import android.app.Activity
    import android.content.Intent
    import android.util.Log
    import android.view.View
    import android.widget.AdapterView
    import android.widget.Spinner
    import android.widget.ArrayAdapter
    import kotlinx.android.synthetic.main.activity_main.*
    import android.widget.AdapterView.OnItemSelectedListener
    import android.widget.Toast

    class MainActivity : AppCompatActivity(), AdapterView.OnItemSelectedListener {

    var spinner1: Spinner? = null
    var spinner2: Spinner? = null
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    spinner1 = findViewById(R.id.spinner1) as Spinner
    spinner2 = findViewById(R.id.spinner2) as Spinner
    val adapter1 = ArrayAdapter.createFromResource(
    this,
    R.array.array1, android.R.layout.simple_spinner_item
    )
    spinner1?.setAdapter(adapter1)
    spinner1?.setOnItemSelectedListener(this)

    val adapter2 = ArrayAdapter.createFromResource(
    this,
    R.array.array1, android.R.layout.simple_spinner_item
    )
    spinner2?.setAdapter(adapter2)
    spinner2?.setOnItemSelectedListener(this)
    }
    override fun onCreateOptionsMenu(menu: Menu): Boolean {
    // Inflate the main; this adds items to the action bar if it is present.
    menuInflater.inflate(R.menu.main, menu)
    return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    val id = item.getItemId()
    return if (id == R.id.action_settings) {
    true
    } else super.onOptionsItemSelected(item)
    }

    override fun onItemSelected(
    parent: AdapterView<*>, view: View, position: Int,
    id: Long
    ) {
    if (spinner1?.getSelectedItem() == "mobile") {
    Toast.makeText(
    applicationContext, "Mobil dipilih",
    Toast.LENGTH_SHORT
    ).show()

    val adapter2 = ArrayAdapter.createFromResource(
    this,
    R.array.mobile_array, android.R.layout.simple_spinner_item
    )
    spinner2?.setAdapter(adapter2)

    } else {
    val adapter2 = ArrayAdapter.createFromResource(
    this,
    R.array.motor_array, android.R.layout.simple_spinner_item
    )
    spinner2?.setAdapter(adapter2)

    }


    // here I try to put the selected contents to variables
    val spinner1_content = spinner1?.getSelectedItem() as String
    textView1.text = spinner1_content
    val spinner2_content = spinner2?.getSelectedItem() as String
    textView2.text = spinner2_content


    }


    override fun onNothingSelected(parent: AdapterView<*>?) {

    }
    }

    最佳答案

    请注意,每次您选择移动设备(或电机)时,您都会重新创建旋转器2。您应该为每个微调器使用不同的监听器:

    spinner1.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {

    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    //your spinner1 listener code
    }

    }

    spinner2.dothesame...

    我把整个 onCreate 放在一起以便更好地理解:

    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    spinner1 = findViewById(R.id.spinner1) as Spinner
    spinner2 = findViewById(R.id.spinner2) as Spinner
    textView1 = findViewById(R.id.textView1) as TextView
    textView2 = findViewById(R.id.textView2) as TextView

    val adapter1 = ArrayAdapter.createFromResource(
    this,
    R.array.array1, android.R.layout.simple_spinner_item
    )
    spinner1?.setAdapter(adapter1)
    spinner1?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {

    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {


    var spinner2Adapter: ArrayAdapter<CharSequence>? = null
    //select the right adapter for de Spinner2
    if (spinner1?.getSelectedItem() == "mobile") {
    spinner2Adapter = ArrayAdapter.createFromResource(
    applicationContext,
    R.array.mobile_array, android.R.layout.simple_spinner_item
    )
    } else {
    spinner2Adapter = ArrayAdapter.createFromResource(
    applicationContext,
    R.array.motor_array, android.R.layout.simple_spinner_item
    )
    }
    //set the adapter and the listener for spinner2
    spinner2?.setAdapter(spinner2Adapter)
    spinner2?.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
    override fun onNothingSelected(parent: AdapterView<*>?) {

    }

    override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
    val spinner2_content = spinner2?.getSelectedItem() as String
    textView2?.text = spinner2_content

    }
    }


    // here I try to put the selected contents to variables
    val spinner1_content = spinner1?.getSelectedItem() as String
    textView1?.text = spinner1_content
    }

    }
    }

    关于java - 如何获取用户在相关微调器上选择的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60533883/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com