Reputation: 29
So I've been trying my best and getting this Spinner to be able to change the list depending on the choices done. For example changing the say a Spinner devoted to floors, would yield a different set of spots when pressing the spot spinner:
XML format:
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="202dp"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight="0.49" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="New Reservation"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/Fname"
android:layout_width="129dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Last Name"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/Lname"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="License"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/License"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<Spinner
android:id="@+id/Garage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Garages"
android:prompt="@string/GaragePrompt" />
<Spinner
android:id="@+id/Floor"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Floors"
android:prompt="@string/FloorPrompt" />
<Spinner
android:id="@+id/Spot"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/Spots"
android:prompt="@string/SpotPrompt" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/Date"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:inputType="date" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Time"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="@+id/Time"
android:layout_width="122dp"
android:layout_height="wrap_content"
android:inputType="time" />
<Button
android:id="@+id/Reserve"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="21dp"
android:text="Reserve" />
</LinearLayout>
</ScrollView>
Thank you for the help.
Upvotes: 0
Views: 454
Reputation: 5244
The following is one way of populating different values on a Spinner
depending on the selection of another Spinner
.
In the following case the Spots
will change values with the selection of the Floor
spinner. This is one way of doing it but might not be the best way...
Declare as class variables...
private Spinner floor, spot;
private ArrayAdapter<String> floorAdapter, spotAdapter;
In onCreate
floor = (Spinner) findViewById(R.id.Floor);
spot = (Spinner) findViewById(R.id.Spot);
// NOTE:
// You can populate the lists below from a resource array as well using ArrayAdapter.createFromResource.
List<String> floor = new ArrayList<String>();
floor.add("spot 1");
floor.add("spot 2");
List<String> spot1 = new ArrayList<String>();
spot1.add("spot1 item 1");
spot1.add("spot1 item 2");
spot1.add("spot1 item 3");
List<String> spot2 = new ArrayList<String>();
spot2.add("spot2 item 1");
spot2.add("spot2 item 2");
spot2.add("spot2 item 3");
floorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, floor);
floor.setAdapter(floorAdapter);
floor.setOnItemSelectedListener(this);
Then do the following on the onItemSelected
for the floor
spinner...
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(position) {
case 0:
spotAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spot1);
spot.setAdapter(spotAdapter);
break;
case 1:
spotAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spot2);
spot.setAdapter(spotAdapter);
break;
default:
break;
}
As I mentioned above, you can load the list from an array as well using ArrayAdapter.createFromResource
.
All the best.
Upvotes: 1