How do I change which String Array my Spinner is using? - android

I'm trying to change the contents of my two Spinners. So I've got my Spinners created:
Spinner spinner01 = (Spinner) findViewById(R.id.Spinner01);
Spinner spinner02 = (Spinner) findViewById(R.id.Spinner02);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
this, R.array.MetricUnitsArray, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner01.setAdapter(adapter1);
spinner02.setAdapter(adapter2);
spinner01.setOnItemSelectedListener(new MyOnItemSelectedListener1());
spinner02.setOnItemSelectedListener(new MyOnItemSelectedListener2());
Now from a different method, I want to be able to do something like this:
adapter1 = ArrayAdapter.createFromResource(
this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
adapter2 = ArrayAdapter.createFromResource(
this, R.array.ImperialUnitsArray, android.R.layout.simple_spinner_item);
Is there anyway to get the adapters to read from a different String Array? Or should I be replacing the values in the String Array with new ones?

Create separate adapters for each array you wish to assign to spinner2.
Declare them as final.
Place the adapter declarations before you set spinner1 ItemSelected Listener.
In spinner1 ItemSelected listener, set the appropriate adapter as needed.
In the code below I have two spinners.
spinner1 has 4 entries containing sensor types, when one of the sensor types is selected spinner2 is then populated with the various models for that sensor type.
public class MainActivity extends Activity {
void showToast(CharSequence msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner s1 = (Spinner) findViewById(R.id.spinner_sensor_type);
final Spinner s2 = (Spinner) findViewById(R.id.spinner_sensor_model);
final ArrayAdapter<CharSequence> sensor_adapter = ArrayAdapter.createFromResource(
this, R.array.sensors,android.R.layout.simple_spinner_item);
sensor_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
final ArrayAdapter<CharSequence> pa_adapter = ArrayAdapter.createFromResource(
this, R.array.pa_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ca_adapter = ArrayAdapter.createFromResource(
this, R.array.ca_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ni_adapter = ArrayAdapter.createFromResource(
this, R.array.ni_models,android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> ni_fe_adapter = ArrayAdapter.createFromResource(
this, R.array.ni_fe_models,android.R.layout.simple_spinner_item);
pa_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ca_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ni_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ni_fe_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(sensor_adapter);
s2.setAdapter(pa_adapter); // set default array values in spinner 2
s1.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Sensor Type: position=" + position + " id=" + id);
switch (position) {
case 0:
s2.setAdapter(pa_adapter);
break;
case 1:
s2.setAdapter(ca_adapter);
break;
case 2:
s2.setAdapter(ni_adapter);
break;
case 3:
s2.setAdapter(ni_fe_adapter);
default: // Do this...
break;
}
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Sensor Type: unselected");
}
});
s2.setOnItemSelectedListener(
new OnItemSelectedListener() {
public void onItemSelected(
AdapterView<?> parent, View view, int position, long id) {
showToast("Sensor Model: position=" + position + " id=" + id);
}
public void onNothingSelected(AdapterView<?> parent) {
showToast("Sensor Model: unselected");
}
});
}
}

You can use the ArrayAdapter's getters/setters:
E.g:
adapter1.clear();
adapter1.addAll(theNewListOfValues);
adapter1.notifyDatasetChanged();

You should be able to just call setAdapter on your spinners with the new adapters. I would suggest storing the adapters as fields in your activity after you create them so you can easily re-use them if you need to swap between the adapters again.

Related

Change content of spinner dynamically to another spinner's item

i have too spinners , spinner and spinner2, first one has a standard menu, the second must change relative to items from spinner.
am trying to get the position of item selected in spinner and then put it as a parametre into my method item2sp to change spinner's 2 items.
but i doesn't change, i keep getting only the first item menu in spinner2, how can i fix it help me please.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
Spinner spinner2;
List<String> categories;
List<String> ndp;
List<String> ps;
ArrayAdapter<String> dataAdapter;
ArrayAdapter<String> ndpAdapter;
ArrayAdapter<String> psAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
// Spinner Drop down elements
categories = new ArrayList<String>();
categories.add("NDP");
categories.add("PS");
categories.add("PA");
categories.add("PC");
categories.add("PH");
ps = new ArrayList<String>();
ps.add("AA");
ps.add("BB");
ps.add("CC");
ndp = new ArrayList<String>();
ndp.add("RP");
ndp.add("AFF");
ndp.add("RPA");
// Creating adapter for spinner
dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
psAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ps);
ndpAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ndp);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
psAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ndpAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
public void itemsp2 (int position){
switch (position){
case 0:
spinner2.setAdapter(ndpAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
case 1:
spinner2.setAdapter(psAdapter);
Toast.makeText(getBaseContext(), ""+position, Toast.LENGTH_LONG).show();
break;
}
}
// int position = spinner.getSelectedItemPosition();
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
itemsp2(position);
// Showing selected spinner item
// Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
iller is my First List and I set this list my firs spinner then I implements onItemSelected method for this spinner. In this method you can add your second spinner with difrent List.
Spinner spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(this);
List<String> iller = new ArrayList<String>();
iller.add("İl Seçiniz..");
iller.add("Antalya");
iller.add("Burdur");
iller.add("Bursa");
iller.add("Balıkesir");
iller.add("Giresun");
iller.add("İstanbul");
ArrayAdapter<String> illerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, iller);
illerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(illerAdapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(View.INVISIBLE);
editText = (EditText) findViewById(R.id.editText);
editText.setVisibility(View.INVISIBLE);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
int itemId = (int) id;
if (itemId == 1) {
iller.remove("İl Seçiniz..");
antalyaİlceler = new ArrayList<String>();
antalyaİlceler.add("İlçe Seçiniz..");
antalyaİlceler.add("Konyaaltı");
antalyaİlceler.add("Muratpaşa");
antalyaİlceler.add("Alanya");
antalyaİlceler.add("Manavgat");
ArrayAdapter<String> antalyaİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, antalyaİlceler);
antalyaİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(antalyaİlcelerAdapter);
spinner2.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
} else if (itemId == 2) {
burdurİlceler = new ArrayList<String>();
burdurİlceler.add("İlçe Seçiniz..");
burdurİlceler.add("Bucak");
burdurİlceler.add("Tefenni");
burdurİlceler.add("Gölhisar");
burdurİlceler.add("Ağlasun");
ArrayAdapter<String> burdurİlcelerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, burdurİlceler);
burdurİlcelerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(burdurİlcelerAdapter);
spinner2.setVisibility(View.VISIBLE);
editText.setVisibility(View.VISIBLE);
}

Conditional entries in Spinner

I am trying to populate entries in a Spinner based on a selected value in another Spinner.
Spinner spin1 = (Spinner) findViewById(R.id.Spinner1);
spin1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Object Obj = parent.getItemAtPosition(pos);
String str = Obj.toString();
Log.i(str, str);
if (str == "B") {
Spinner spin2 = (Spinner) findViewById(R.id.Spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getApplicationContext(), R.array.array1,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter);
} else if (str=="M") {
Spinner spin2 = (Spinner) findViewById(R.id.Spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getApplicationContext(), R.array.array2,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(adapter);
}
}
I have assembled this code after going through different forums. But this is not working for me. The Log.i is giving me the correct city as selected but the second spinner is not getting populated with the new array.
You can not compare strings using ==, and your condition if(str == "B") will never be true.
Try using equalsIgnoreCase or equals
if(str.equals("B"))
{
//populate second spinner
}

Spinners not working as they should, How to resolve?

Hey guys I have code shown bellow, which is supposed to get the item selected from the first spinner - Poets and then show another spinner depending on input poem 1,2 or 3. Once An item is selected from the second spinner it should then show a text view.
My problem is that sometimes it works, sometimes it chooses randomly which spinner to show it would seem and almost every single time when I select an item from spinner 2, the text view is almost never shown and when it is it's normally text view entitled tree.
can anyone help? I do not see any errors so I am confused
code:
public class Poets extends Activity implements OnItemSelectedListener{
//defining variables
Spinner PoetSpinner, PoemSpinner1, PoemSpinner2, PoemSpinner3;
String Poets[] = {"","Robert Burns", "James Beattie", "Duncan MacGregor Crerar"};
String Poems1[] ={"", "Tam O Shanter", "Auld Lang Syne"};
String Poems2[] ={"", "Judgement Of Paris", "The Minstrel"};
String Poems3[] ={"", "A Poem Commemorating Robbie Burns", "My Bonnie Rowan Tree"};
TextView Tam, Auld, Judge, TheMin, rob, tree ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set to fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN );
//set content view
setContentView(R.layout.poet);
initialize(); //call this method
}
public void initialize(){
//initializing items that are in the class
PoetSpinner = (Spinner) findViewById(R.id.spPoet);
PoemSpinner1 = (Spinner) findViewById(R.id.spPoem1);
PoemSpinner2 = (Spinner) findViewById(R.id.spPoem2);
PoemSpinner3 = (Spinner) findViewById(R.id.spPoem3);
Tam = (TextView) findViewById(R.id.tvSelectPoem1);
Auld = (TextView) findViewById(R.id.tvSelectPoem2);
Judge = (TextView) findViewById(R.id.tvSelectPoem3);
TheMin = (TextView) findViewById(R.id.tvSelectPoem4);
rob = (TextView) findViewById(R.id.tvSelectPoem5);
tree = (TextView) findViewById(R.id.tvSelectPoem6);
//setting up array adapters
//
//for each spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.poet_arrays, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.poem_arrays1, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter3 = ArrayAdapter.createFromResource(this, R.array.poem_arrays2, android.R.layout.simple_spinner_item);
ArrayAdapter<CharSequence> adapter4 = ArrayAdapter.createFromResource(this, R.array.poem_arrays3, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
PoetSpinner.setAdapter(adapter);
PoetSpinner.setOnItemSelectedListener(this);
PoemSpinner1.setAdapter(adapter2);
PoemSpinner1.setOnItemSelectedListener(this);
PoemSpinner2.setAdapter(adapter3);
PoemSpinner2.setOnItemSelectedListener(this);
PoemSpinner3.setAdapter(adapter4);
PoemSpinner3.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
String val = Poets[pos];
String val2 = Poems1[pos];
String val3 = Poems2[pos];
String val4 = Poems3[pos];
if (val.equals("Robert Burns")) {
PoemSpinner2.setVisibility(Spinner.GONE);
PoemSpinner1.setVisibility(Spinner.VISIBLE);
PoemSpinner3.setVisibility(Spinner.GONE);
}
if (val.equals("James Beattie")) {
PoemSpinner2.setVisibility(Spinner.VISIBLE);
PoemSpinner1.setVisibility(Spinner.GONE);
PoemSpinner3.setVisibility(Spinner.GONE);
}
if (val.equals("Duncan MacGregor Crerar")) {
PoemSpinner2.setVisibility(Spinner.GONE);
PoemSpinner1.setVisibility(Spinner.GONE);
PoemSpinner3.setVisibility(Spinner.VISIBLE);
}
if (val2.equals("Tam O Shanter")) {
Tam.setVisibility(View.VISIBLE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
tree.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val2.equals("Auld Lang Syne")) {
Auld.setVisibility(View.VISIBLE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val3.equals("Judgment Of Paris")) {
Judge.setVisibility(View.VISIBLE);
TheMin.setVisibility(View.GONE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val3.equals("The Minstrel")) {
TheMin.setVisibility(View.VISIBLE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
if (val4.equals("A Poem Commemorating Robbie Burns")) {
rob.setVisibility(View.VISIBLE);
tree.setVisibility(View.GONE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
}
if (val4.equals("My Bonnie Rowan Tree"))
tree.setVisibility(View.VISIBLE);
Tam.setVisibility(View.GONE);
Auld.setVisibility(View.GONE);
Judge.setVisibility(View.GONE);
TheMin.setVisibility(View.GONE);
rob.setVisibility(View.GONE);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
I have a feeling you have overcomplicated things a little. You can make what you want with 2 spinners and only one textview.
The first spinner will contain the poets, and based on the poet selected in this spinner you can modify the content of the second spinner in which you can have the poems of the selected poet. You can then show the selected poems name in the textview when the poem is selected in the second spinner. I would try with something like this:
public class Poets extends Activity implements
OnItemSelectedListener {
// defining variables
Spinner PoetSpinner, PoemSpinner;
String Poets[] = { "", "Robert Burns", "James Beattie",
"Duncan MacGregor Crerar" };
String Poems1[] = { "", "Tam O Shanter", "Auld Lang Syne" };
String Poems2[] = { "", "Judgement Of Paris", "The Minstrel" };
String Poems3[] = { "", "A Poem Commemorating Robbie Burns",
"My Bonnie Rowan Tree" };
TextView selectedPoem;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set to fullscreen mode
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// set content view
setContentView(R.layout.poet);
initialize(); // call this method
}
public void initialize() {
// initializing items that are in the class
PoetSpinner = (Spinner) findViewById(R.id.spPoet);
PoemSpinner = (Spinner) findViewById(R.id.spPoem);
// Hide the second spinner
PoemSpinner.setVisibility(View.GONE);
selectedPoem = (TextView) findViewById(R.id.tvSelectPoem1);
// setting up array adapters
//
// for each spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter
.createFromResource(this, R.array.poet_arrays,
android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
PoetSpinner.setAdapter(adapter);
PoetSpinner.setOnItemSelectedListener(this);
PoemSpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
Object poem = parent.getItemAtPosition(position);
selectedPoem.setText(poem.toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
ArrayAdapter<CharSequence> adapter = null;
switch (pos) {
case 1:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays1, android.R.layout.simple_spinner_item);
break;
case 2:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays2, android.R.layout.simple_spinner_item);
break;
case 3:
adapter = ArrayAdapter.createFromResource(this,
R.array.poem_arrays3, android.R.layout.simple_spinner_item);
break;
default:
// Hide the poem spinner
PoemSpinner.setVisibility(View.GONE);
break;
}
if (adapter != null) {
// Set the poem spinners content and show the spinner
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
PoemSpinner.setAdapter(adapter);
PoemSpinner.setVisibility(View.VISIBLE);
}
}
public void onNothingSelected(AdapterView<?> parent) {
}
}
Of course you will have to delete the unused views from your xml.
I hope i was clear enough, and the code works as you wanted to.

spinner dependent on another spinner in android

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.

chanding contents of spinners run-time (android), tried and failed

I recently posted a question on how to change the values of a spinner during program execution and was told to change the array used to make the adaptor and call notifyDataSetChanged();
I tried that but my spinner is not getting updated even though my array is. I attach the code below
public void onCreate(Bundle savedInstanceState)
{
res=getResources();
Boolean a;
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// First spinner whose value determines the value of the second spinner
spinner = (Spinner) findViewById(R.id.spinner1);
// ArrayAdaptor of first spinner
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource( this R.array.planets_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner2.setVisibility(4);// spinner 2 is not visible initially
// ArrayAdaptor of first spinner
adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinner_drop);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
Context context = getApplicationContext();
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(pos!=0)
{
//copying string array of second spinner from strings.xml to the current string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0] , Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.refreshDrawableState();
spinner2.setVisibility(0);//this command works and the spinner is visible, but it is empty
}
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
Any idea what I am doing wrong any ideas will help.
PS: I have tried removing the entire invisible, visible thing, doesnt help
Thanks in advance
Modify your onItemSelected method as follows, note the new addition I added below.
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (pos!=0) {
//copying string array of second spinner from strings.xml to the current
//string array
spinner_drop=res.getStringArray(R.array.activities_array2);
Toast.makeText(parent.getContext(), "The planet is " +spinner_drop[0],
Toast.LENGTH_SHORT).show(); //this toast shows up
adapter2.notifyDataSetChanged();
spinner2.setAdapter(adapter2); // <--- New Addition
// spinner2.refreshDrawableState();
spinner2.setVisibility(0); //this command works and the spinner is visible,
//but it is empty
}
}

Categories

Resources