Что я хотел бы сделать
Я хотел бы получить данные, которые пользователь выбрал на зависимых счетчиках.
В моем текущем коде удалось получить данные из первого счетчик, но я не могу сделать это со второго счетчика.
Проблема
Как мне исправить свой код, чтобы получить соответствующие данные по выбору пользователя?
- В этом В этом случае вторым textView должен быть BMW, но это Audi, который является первым кандидатом на второй счетчик.
Первый текстовый вид изменяется после выбора пользователя. В этом случае он изменился на мобильный с двигателя, но снова второй текстовый вид остался первым первым кандидатом на второй счетчик.
Текущий код
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<*>?) {
}
}