Reputation: 79
I'm building a simple app for android which consists of spinners and edittext.
question is I have 2 spinners with arrays in an adapter
Spinner 1
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.kendaraan_arrays,android.R.layout.simple_spinner_item);
spKendaraan.setAdapter(adapter1);
what I want is arrays from spinner 2 is depends on what i select in spinner 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);
}
}
my code above doesn't work, am I making logic/method mistakes?
tl;dr
if spinnerA == x{
spinnerB = m
}
else {
spinnerB = n
}
since i'm new in android, any example would be very appreciated.
Upvotes: 0
Views: 12301
Reputation: 216
I don't know why your code is not working but following is a example code for you which works as you needed,I hope this will help.
XML layout file:
<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>
Activity:
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
}
}
Strings:
<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>
Upvotes: 6
Reputation: 21937
I just can see that below this line instead of this
you could write getApplicationContext()
or getBaseContext()
So the actual code should be inside onItemSelected
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(getBaseContext(), R.array.pelanggaran_mobil, android.R.layout.simple_spinner_item);
I think it should work right now. Please let me know if you get any problem with this :)
Upvotes: 1