i want to change the spinner-array of my Spinner2 by selecting something on my Spinner1.
It means if i change the selecteted item of spinner1 to position 2,3 etc. the listener has to change the whole items of my spinner2. (array in Strings.xml)
I tried to set it to another adapter but doesn't work...
Can anyone help me? thx
Here's my code:
public class DamenBH extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
Spinner spinner1;
Spinner spinner2;
ArrayAdapter<CharSequence> adapter1;
ArrayAdapter<CharSequence> adapter65, adapter70, adapter75;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_damen_b_h);
spinner1 = findViewById(R.id.spinner1);
adapter1 = ArrayAdapter.createFromResource(this, R.array.beispiel1, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner2 = findViewById(R.id.spinner2);
spinner2.setOnItemSelectedListener(this);
adapter65 = ArrayAdapter.createFromResource(this, R.array.beispiel2, android.R.layout.simple_spinner_item);
adapter65.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner2.setAdapter(adapter65);
adapter70 = ArrayAdapter.createFromResource(this, R.array.beispiel3, android.R.layout.simple_spinner_item);
adapter70.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
adapter75 = ArrayAdapter.createFromResource(this, R.array.beispiel4, android.R.layout.simple_spinner_item);
adapter75.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
spinner1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 0:
spinner2.setAdapter(adapter65);
break;
case 1:
spinner2.setAdapter(adapter70);
break;
case 2:
spinner2.setAdapter(adapter75);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I came up with this and it seems to work fine for me.
package com.example.energydrinkapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import java.lang.reflect.Array;
public class EnterInfoActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner manufacturer;
ArrayAdapter<CharSequence> manufacturerAdapter;
Spinner flavor;
ArrayAdapter<CharSequence> flavorAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_enter_info);
manufacturer = (Spinner) findViewById(R.id.spinner_manufacturer);
manufacturer.setOnItemSelectedListener(this);
manufacturerAdapter = ArrayAdapter.createFromResource(this, R.array.manufacturer_array, android.R.layout.simple_spinner_item);
manufacturerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
manufacturer.setAdapter(manufacturerAdapter);;
}
public void flavorArraySet(int flavors){
flavor = (Spinner) findViewById(R.id.spinner_flavor);
flavorAdapter = ArrayAdapter.createFromResource(this, flavors, android.R.layout.simple_spinner_item);
flavorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
flavor.setAdapter(flavorAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (position == 0){
Log.e("array","none");
}
else if (position == 1){
Log.e("array", "monster");
flavorArraySet(R.array.monster_array);
}
else if (position == 2){
Log.e("array", "red bull");
flavorArraySet(R.array.redbull_array);
}
else if (position == 3){
Log.e("array", "reign");
flavorArraySet(R.array.reign_array);
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
Related
I have a fragment that loads 3 spinners and a button. The list array of the second and third spinner is determined by the value of spinner 1 so when you change spinner 1, the arrays of spinners 2 and 3 change on select.
This works but I'm now struggling to retrieve the string value (and ultimately the relative numerical value) of spinner 2 on the click of the button.
In order to test my code I'm just using Toast to display the string.
The ultimate goal is to have a calculation in the background (e.g. spinner 2 multiplied by spinner 3) but when I use OnClick, the string of spinner 2 at time of onCreate is displayed.
I have tried to put a switch in OnItemSelectedListener to attribute this to both spinners but it's not worked.
Really, I want to click the button and the string of spinner 2 is displayed in toast.
Here's my code:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class Convertor extends Fragment {
public Convertor() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View view=inflater.inflate(R.layout.convertor_1, container, false);
String [] values_conversion = {"Convert1","Convert2",};
final Spinner spinner1 = view.findViewById(R.id.spinner1);
final Spinner spinner2 = view.findViewById(R.id.spinner2);
final Spinner spinner3 = view.findViewById(R.id.spinner3);
final ArrayAdapter adapter1 = ArrayAdapter.createFromResource(getActivity(), R.array.array1_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter adapter2 = ArrayAdapter.createFromResource(getActivity(), R.array.array2_array, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter adapter3 = ArrayAdapter.createFromResource(getActivity(), R.array.array3_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter adapter4 = ArrayAdapter.createFromResource(getActivity(), R.array.array4_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter1);
spinner3.setAdapter(adapter3);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, values_conversion);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spinner1.setAdapter(adapter);
spinner1.setSelection(0);
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, final View view, int position,
long id) {
switch (position) {
case 0:
spinner2.setAdapter(adapter1);
spinner3.setAdapter(adapter3);
break;
case 1:
spinner2.setAdapter(adapter2);
spinner3.setAdapter(adapter4);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
final String from_unit1 = spinner2.getSelectedItem().toString(); // This gives the from string
final Button convert_btn = (Button) view.findViewById(R.id.convert_btn);
convert_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast toast = Toast.makeText(getContext(), from_unit1, Toast.LENGTH_SHORT);
toast.show();
}
});
return view;
}
}
try this
final Button convert_btn = (Button) view.findViewById(R.id.convert_btn);
convert_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String from_unit1 = spinner2.getSelectedItem().toString();
Toast toast = Toast.makeText(getContext(), from_unit1, Toast.LENGTH_SHORT);
toast.show();
}
});
I am new to android, I am trying to implement the cascading spinner. When I select the first spinner it should populate the second spinner choices based on the 1st spinner selection. I have almost done, but in second spinner I cannot select the value. When I select the value it is disappearing. Please help me in this.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class DummyMain extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
public Spinner spinner1, spinner2;
public String[] spinner1Values = {"A", "B"}, valuesForA = {"1", "2"}, valuesForB = {"3", "4", "5", "1"};
public ArrayAdapter<String> adapterForSpinner1, adapterForSpinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy_main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapterForSpinner1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinner1Values);
adapterForSpinner1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapterForSpinner1);
spinner1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int i, long l) {
Toast.makeText(this, "Result "+spinner1.getSelectedItem().toString(), Toast.LENGTH_SHORT).show();
if (parent.getId() == R.id.spinner1) {
if (spinner1.getSelectedItem().toString().equals("A")) {
adapterForSpinner2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item,valuesForA);
adapterForSpinner2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterForSpinner2);
spinner2.setOnItemSelectedListener(this);
} else {
adapterForSpinner2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item,valuesForB);
adapterForSpinner2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapterForSpinner2);
spinner2.setOnItemSelectedListener(this);
}
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Found the issue, I have fixed it.
ArrayAdapter<String> adapterForSpinner2 = new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_spinner_item, valuesForB);
I have changed the getApplicationConetxt() into this. It works :-)
ArrayAdapter<String> adapterForSpinner2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, valuesForB);
try adding this in your DummyMain
#Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
I've got numbers from 1 to 12 in spinner1, and another spinner2 that I'd like to be controlled by what the user picked in the former.
Whenever user selects a number between from 1 to 6 in spinner1, spinner2 should show numbers from 1 to 31.
Whenever user selects a number between from 7 to 12 in spinner1, spinner2 should show numbers from 1 to 30.
I've managed to come up with this code, but it does not seem to work as above. Could help me in finding what's wrong?
package com.example.spinners;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity {
Spinner sp1, sp2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1 = (Spinner) findViewById(R.id.SP1);
sp2 = (Spinner) findViewById(R.id.SP2);
List<String> list = new ArrayList<String>();
list.add("1"); list.add("2"); list.add("3"); list.add("4");
list.add("5"); list.add("6"); list.add("7"); list.add("8");
list.add("9"); list.add("10"); list.add("11"); list.add("12");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(dataAdapter);
//
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// userSelectedIndex = position;
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
//
if (sp1.getSelectedItem().equals("1") || sp1.getSelectedItem().equals("2")
|| sp1.getSelectedItem().equals("3") || sp1.getSelectedItem().equals("4")
|| sp1.getSelectedItem().equals("5") || sp1.getSelectedItem().equals("6")) {
//
List<String> list2 = new ArrayList<String>();
list2.add("1"); list2.add("2"); list2.add("3"); list2.add("4"); list2.add("5");
list2.add("6"); list2.add("7"); list2.add("8"); list2.add("9"); list2.add("10");
list2.add("11"); list2.add("12"); list2.add("13"); list2.add("14");
list2.add("15"); list2.add("16"); list2.add("17"); list2.add("18");
list2.add("19"); list2.add("20"); list2.add("21"); list2.add("22");
list2.add("23"); list2.add("24"); list2.add("25"); list2.add("26");
list2.add("27"); list2.add("28"); list2.add("29"); list2.add("30");
list2.add("31");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(dataAdapter2);
} else if(sp1.getSelectedItem().equals("7") || sp1.getSelectedItem().equals("8") || sp1.getSelectedItem().equals("9") || sp1.getSelectedItem().equals("10") || sp1.getSelectedItem().equals("11") || sp1.getSelectedItem().equals("12")) {
/////////////////////////////////////////////////////////
//
List<String> list2 = new ArrayList<String>();
list2.add("1"); list2.add("2"); list2.add("3"); list2.add("4"); list2.add("5");
list2.add("6"); list2.add("7"); list2.add("8"); list2.add("9"); list2.add("10");
list2.add("11"); list2.add("12"); list2.add("13"); list2.add("14");
list2.add("15"); list2.add("16"); list2.add("17"); list2.add("18");
list2.add("19"); list2.add("20"); list2.add("21"); list2.add("22");
list2.add("23"); list2.add("24"); list2.add("25"); list2.add("26");
list2.add("27"); list2.add("28"); list2.add("29"); list2.add("30");
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(dataAdapter2);
}
//
}
}
The problem in your code is, that you are checking the selected value only once, before the window has even opened. You need to check it every time a user changes the value. So you need that logic in your onItemSelectedListener. Here the revised code, with a few improvements (untested, but it should work).
public class MainActivity extends Activity {
private Spinner sp1, sp2;
private List<Integer> firstList = new ArrayList<Integer>();
private List<Integer> secondList = new ArrayList<Integer>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp1 = (Spinner) findViewById(R.id.SP1);
sp2 = (Spinner) findViewById(R.id.SP2);
for (int i = 1; i <= 12; i++) {
firstList.add(i);
}
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<Integer>(this, android.R.layout.simple_spinner_item, firstList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(dataAdapter);
sp1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int currentValue = firstList.get(position);
if(currentValue <= 6){
for (int i = 1; i <= 30; i++) {
secondList.add(i);
}
} else {
for (int i = 1; i <= 31; i++) {
secondList.add(i);
}
}
ArrayAdapter<Integer> dataAdapter = new ArrayAdapter<Integer>(MainActivity.this, android.R.layout.simple_spinner_item, secondList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(dataAdapter);
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I have this code so far. The Model Spinner automatically populates according to what the user picks for the Manufacture. (For the code below I only have it set up for BMW). Is there any way for the third spinner (Color) to be populated based on the Manufacture and Model.
So if you select BMW on the first spinner and then you select M3 coupe for the second spinner the third spinner would show all the option in the M3_Color Array.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends Activity implements OnItemSelectedListener {
private String[] Manufacture { "Ford",
"BMW",
"Nissan",
"Toyota",
"Chevy",
};
private String[] BMW_Model = { "128i Coupe",
"M3 Coupe",
"M5 Sedan",
"640i Gran Coupe",
"335i Coupe",
};
private String[] M3_Color = { "Jerez Black Metallic",
"Melbourne Red Metallic",
"Mineral White Metallic",
"Le Mans Blue Metallic",
};
Spinner spinnerManufacture, spinnerModel, spinnerColor;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerManufacture = (Spinner) findViewById(R.id.manufacture_spinner);
ArrayAdapter<String> MaufactureAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,Manufacture);
MaufactureAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerManufacture.setAdapter(MaufactureAdapter);
spinnerManufacture.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,long id) {
// TODO Auto-generated method stub
{
if(parent.getId() == R.id.maufacture_spinner && position == 1)
{
spinnerModel = (Spinner) findViewById(R.id.model_spinner);
ArrayAdapter<String> ModelAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, BMW_Model);
ModelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerModel.setAdapter(ModelAdapter);
if(parent.getId() == R.id.model_spinner)
{
String[] Color;
if(spinnerMaufacture.getSelectedItem().toString.equals("BMW"))
{
if(spinnerModel.getSelectedItem().toString.equals("M3 Coupe))
{
Color = M3_Color
}
}
spinnerColor = (Spinner) findViewById(R.id.color_spinner);
ArrayAdapter<String> ColorAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, M3_Color);
ColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(ColorAdapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
you need to move if(parent.getId() == R.id.model_spinner) outside if(parent.getId() == R.id.maufacture_spinner then makesure and call spinnerModel.setOnItemSelectedListener(this) onCreate, to do that you need to move spinnerModel = (Spinner) findViewById(R.id.model_spinner) into your on create, I'd recommend doing the same for all your views, that way you don't call findViewById every time you select a spinner item.
EDIT:
Spinner spinnerManufacture, spinnerModel, spinnerColor;
private ArrayAdapter<String> modelAdapter;
private ArrayAdapter<String> maufactureAdapter;
private ArrayAdapter<String> colorAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinnerManufacture = (Spinner) findViewById(R.id.manufacture_spinner);
spinnerModel = (Spinner) findViewById(R.id.model_spinner);
spinnerColor = (Spinner) findViewById(R.id.color_spinner);
maufactureAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, Manufacture);
modelAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
colorAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item);
maufactureAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
modelAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
colorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(colorAdapter);
spinnerManufacture.setAdapter(maufactureAdapter);
spinnerModel.setAdapter(modelAdapter);
spinnerManufacture.setOnItemSelectedListener(this);
spinnerModel.setOnItemSelectedListener(this);
spinnerColor.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.maufacture_spinner) {
String[] model;
if (spinnerManufacture.getSelectedItem().toString().equals("BMW")){
model = BMW_Model;
} else {
model = new String[] {"not sure what model list to use"};
}
modelAdapter.clear();
modelAdapter.addAll(model);
}
if (parent.getId() == R.id.model_spinner) {
String[] Color;
if (spinnerModel.getSelectedItem().toString().equals("M3 Coupe")) {
Color = M3_Color;
} else if (spinnerModel.getSelectedItem().toString().equals("M5 Sedan")) {
Color = M5_Color;
} else {
Color = new String[] { "not sure what color list to use" };
}
colorAdapter.clear();
colorAdapter.addAll(Color);
}
}
this is what I recommend you try.
Just build the string based on what is selected like below. This sets the color list as M3_Color for BMW and M3 Coupe being selected, and M5_Color when BMW and M5 Sedan are selected. This would need to be done for all possible combinations.
if(parent.getId() == R.id.model_spinner)
{
String[] Color = new String[];
if(spinnerManufacture.getSelectedItem().toString().equals("BMW"))
{
if(spinnerModel.getSelectedItem().toString().equals("M3 Coupe"))
{
Color = M3_Color;
}
else if(spinnerModel.getSelectedItem().toString().equals("M5 Sedan"))
{
Color = M5_Color;
}
}
spinnerColor = (Spinner) findViewById(R.id.color_spinner);
ArrayAdapter<String> ColorAdapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, Color);
ColorAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColor.setAdapter(ColorAdapter);
}
http://developer.android.com/resources/tutorials/views/hello-spinner.html
I use this link as the base..
I have 3 spinners, first spinner for country , second for states and third for cities.
I had already seen the links on the stackoverflow , but it is not that much proper.
my code is as follows
public class SpinnerActivity extends Activity
{
String state="";
ArrayAdapter adapter1,adapter2;
Spinner spinner1,spinner2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter1 = ArrayAdapter.createFromResource(
this, R.array.states_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
if(state.equalsIgnoreCase("Gujarat"))
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
if(state == "Rajasthan")
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
state=parent.getItemAtPosition(pos).toString();
Toast.makeText(parent.getContext(), "The state is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
}
the bug I found is , I have created all he thing in the onCreate() method..
but I do not find the alternate thing that can I do ...
I don't know why this not work ..
please help if possible
thanks...
In onSelectItem fill second spinner and so on.