我正在构建一个简单的Android应用程序,它由微调器和edittext组成。
问题是我在适配器中有2个带有阵列的旋转器
Spinner 1
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.kendaraan_arrays,android.R.layout.simple_spinner_item);
spKendaraan.setAdapter(adapter1);
我想要的是来自微调器2的数组取决于我在微调器1中选择的内容
Spinner 2
@Override
public void onItemSelected (AdapterView < ? > adapterView, View view,int position, long id){
if (spKendaraan.getSelectedItem().equals("Mobil")) {
Toast.makeText(getApplicationContext(), "Mobil dipilih", Toast.LENGTH_SHORT).show();
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.pelanggaran_mobil, android.R.layout.simple_spinner_item);
spPelanggaran.setAdapter(adapter2);
} else {
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this, R.array.pelanggaran_motor, android.R.layout.simple_spinner_item);
spPelanggaran.setAdapter(adapter2);
}
}
上面的代码不起作用,我是否犯了逻辑/方法错误?
TL;博士
if spinnerA == x{
spinnerB = m
}
else {
spinnerB = n
}
因为我是android的新手,所以任何一个例子都会非常感激。
答案 0 :(得分:2)
我不知道为什么你的代码不能正常工作,但是以下代码是适合您的示例代码,我希望这会有所帮助。
XML布局文件:
<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" />
</RelativeLayout>
的活动:
public class MainActivity extends Activity implements OnItemSelectedListener {
Spinner spinner1, spinner2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,
R.array.array1, android.R.layout.simple_spinner_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// 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.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if (spinner1.getSelectedItem().equals("mobile")) {
Toast.makeText(getApplicationContext(), "Mobil dipilih",
Toast.LENGTH_SHORT).show();
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,
R.array.mobile_array, android.R.layout.simple_spinner_item);
spinner2.setAdapter(adapter2);
} else {
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,
R.array.motor_array, android.R.layout.simple_spinner_item);
spinner2.setAdapter(adapter2);
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
// TODO Auto-generated method stub
}
}
的字符串:
<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>
答案 1 :(得分:0)
我可以看到在这一行而不是this
下面你可以写getApplicationContext()
或getBaseContext()
所以实际代码应该在onItemSelected
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(getBaseContext(), R.array.pelanggaran_mobil, android.R.layout.simple_spinner_item);
我认为它现在应该可行。如果您对此有任何疑问,请与我们联系:)