So i have made a stat calculator app for a game where the user selects a combination through various spinners and then clicks a button to calculate the resulting stats. So now i want to remove the button entirely and have the resulting stats update automatically when the item in the spinner changes. Here is my spinner listener code:
public class MySpinnerListener implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
int identifier = arg0.getId();
String id = arg0.getItemAtPosition(arg2).toString();
id=id.replaceAll("\\s+","");
Data data= Data.valueOf(id);
switch (data) {
//big case switch to find what is clicked an update the variables accordingly..
And here is my calculate function
public void Calculate() {
calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
//creates object with all the stats (there are two because the user can compare different combinations) ChangeColor just makes it look nice.
TotalStats stats1 = new TotalStats(player, car, wheel, glide);
TotalStats stats2 = new TotalStats(player2, car2, wheel2,
glide2);
ChangeColor leftStats= new ChangeColor(stats1);
ChangeColor rightStats= new ChangeColor(stats2);
//outputs the stats
statsView1.setText(leftStats.builder, BufferType.SPANNABLE);
statsView2.setText(rightStats.builder,BufferType.SPANNABLE);
}
});
EDIT: Here is my onCreate method and my initial setup.
Spinner character, character1;
Spinner vehicle, vehicle1;
Spinner wheels, wheels1;
Spinner glider, glider1;
Button calculate;
TextView statsView1, statsView2, statsView, title;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.fragment_main);
statsView = (TextView) findViewById(R.id.statsView);
statsView1 = (TextView) findViewById(R.id.stats1);
statsView2 = (TextView) findViewById(R.id.stats2);
title=(TextView)findViewById(R.id.title);
Typeface font = Typeface.createFromAsset(getAssets(),"New Super Mario Font U.ttf");
statsView.setTypeface(font);
statsView1.setTypeface(font);
statsView2.setTypeface(font);
title.setTypeface(font);
InitialSetupUI();
Calculate();
}
private void InitialSetupUI() {
// TODO Auto-generated method stub
character = (Spinner) findViewById(R.id.character);
vehicle = (Spinner) findViewById(R.id.vehicle);
wheels = (Spinner) findViewById(R.id.wheels);
glider = (Spinner) findViewById(R.id.glider);
character1 = (Spinner) findViewById(R.id.character2);
vehicle1 = (Spinner) findViewById(R.id.vehicle2);
wheels1 = (Spinner) findViewById(R.id.wheels2);
glider1 = (Spinner) findViewById(R.id.glider2);
calculate = (Button) findViewById(R.id.calculate);
ArrayAdapter cadapter = ArrayAdapter.createFromResource(this,
R.array.character_array, R.layout.spinner_item);
ArrayAdapter vadapter = ArrayAdapter.createFromResource(this,
R.array.vehicle_array, R.layout.spinner_item);
ArrayAdapter wadapter = ArrayAdapter.createFromResource(this,
R.array.wheels_array, R.layout.spinner_item);
ArrayAdapter gadapter = ArrayAdapter.createFromResource(this,
R.array.glider_array, R.layout.spinner_item);
character.setAdapter(cadapter);
vehicle.setAdapter(vadapter);
wheels.setAdapter(wadapter);
glider.setAdapter(gadapter);
character1.setAdapter(cadapter);
vehicle1.setAdapter(vadapter);
wheels1.setAdapter(wadapter);
glider1.setAdapter(gadapter);
vehicle.setOnItemSelectedListener(new MySpinnerListener());
character.setOnItemSelectedListener(new MySpinnerListener());
wheels.setOnItemSelectedListener(new MySpinnerListener());
glider.setOnItemSelectedListener(new MySpinnerListener());
vehicle1.setOnItemSelectedListener(new MySpinnerListener());
character1.setOnItemSelectedListener(new MySpinnerListener());
wheels1.setOnItemSelectedListener(new MySpinnerListener());
glider1.setOnItemSelectedListener(new MySpinnerListener());
}
Related
I am new to android,and in this code i have two spinners and based on the values selected,different layouts should open accordingly. Im stuck with what logic should i use to open different layouts, inside the OnItemSelected for the spinner.Say i have 5 items in spinner and i have 5 different layouts for each item.So how should i do??
Any help or ideas would be much appreciated. Thanks in advance.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(i);
}
});
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Spinner spinner1 = (Spinner)parent;
Spinner spinner2 = (Spinner)parent;
if(spinner1.equals(position))
{
}
if(spinner2.getId() == R.id.spinner2)
{
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
Try below.
check the onClickListener of button which will decide which activity to open.
Spinner spinner1;
Spinner spinner2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
// Spinner element
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
Button button = (Button) findViewById(R.id.button);
// Spinner click listener
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Maruti");
categories.add("Ford");
categories.add("Nissan");
categories.add("TATA");
List<String> types = new ArrayList<String>();
types.add("1Lakh-5Lakh");
types.add("5Lakh-10Lakh");
types.add("10Lakh-20Lakh");
types.add("20Lakh-50Lakh");
types.add("Above 50Lakhs");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> spinner2Adapter=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,types);
spinner2Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner1.setAdapter(dataAdapter);
spinner2.setAdapter(spinner2Adapter);
spinner1.setOnItemSelectedListener(this);
spinner2.setOnItemSelectedListener(this);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent i;
if(spinner1_selected_item.equals("Maruti"))
{
if(spinner2_selected_item.equals("1Lakh-5Lakh"))
i=new Intent(SecondActivity.this,ThirdActivity.class);
if(spinner2_selected_item.equals("5Lakh-10Lakh"))
i=new Intent(SecondActivity.this,FourthActivity.class);
if(spinner2_selected_item.equals("10Lakh-20Lakh"))
i=new Intent(SecondActivity.this,FifthActivity.class);
}
startActivity(i);
}
});
}
String spinner1_selected_item;
String spinner2_selected_item;
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(spinner1.getId() == R.id.spinner1)
{
spinner1_selected_item = parent.getItemAtPosition(position).toString();
}
if(spinner2.getId() == R.id.spinner2)
{
spinner2_selected_item = parent.getItemAtPosition(position).toString();
}
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}}
I have a spinner2 which will have day and month, when user select day, s1 and s2 will display hours and mins respectively.
On startup of the activity, I have the daily selected.
The problem is I keep on getting toast of daily. Which suggested that it gets selecteditem endlessly. Also, I cant select values in s1 and s2, the spinner keep on refreshing among themselves. How to solve this?
public class try_spinner extends Activity implements AdapterView.OnItemSelectedListener {
Spinner spinner1, spinner2, s1, s2, s3;
String app_name, room_name;
TextView app, room;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.try_spinner);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int) (width * .9), (int) (height * .7));
Bundle extras = getIntent().getExtras();
final String app_string = extras.getString("APPLIANCE_NAME");
final String room_string = extras.getString("APPLIANCE_ROOM");
app = (TextView) findViewById(R.id.app_name);
app.setText(app_string);
room = (TextView) findViewById(R.id.room_name);
room.setText(room_string);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
spinner2 = (Spinner) findViewById(R.id.spinner2);
s1 = (Spinner) findViewById(R.id.s1);
s2 = (Spinner) findViewById(R.id.s2);
s3 = (Spinner) findViewById(R.id.s3);
spinner2.setOnItemSelectedListener(this);
s1.setOnItemSelectedListener(this);
s2.setOnItemSelectedListener(this);
s3.setOnItemSelectedListener(this);
if (app_string.equals("Lighting")) {
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.lighting));
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
spinner1.setAdapter(dataAdapter2);
} else if (app_string.equals("Shower Heater")) {
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.shower_heater));
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
spinner1.setAdapter(dataAdapter2);
} else {
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter2.notifyDataSetChanged();
spinner1.setAdapter(dataAdapter2);
}
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String txt_spinner2 = String.valueOf(spinner2.getSelectedItem());
Toast.makeText(this, txt_spinner2, Toast.LENGTH_SHORT).show();
if (txt_spinner2.contentEquals("Daily")) {
List<String> list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.days));
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter.notifyDataSetChanged();
s1.setAdapter(dataAdapter);
String txt_s1 = String.valueOf(s1.getSelectedItem());
//set for daily hours usage
List<String> list3 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter3.notifyDataSetChanged();
s2.setAdapter(dataAdapter3);
//set for daily minutes usage
List<String> list4 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter4 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.hours));
dataAdapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter4.notifyDataSetChanged();
s2.setAdapter(dataAdapter4);
//set for daily empty thrd space
List<String> list5 = new ArrayList<String>();
ArrayAdapter<String> dataAdapter5 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, getResources().getStringArray(R.array.empty));
dataAdapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter5.notifyDataSetChanged();
s3.setAdapter(dataAdapter5);
}
else if (txt_spinner2.contentEquals("Monthly")) {
List<String> list = new ArrayList<String>();
list.add("Conveyance");//you should add items from db here(2nd spinner)
list.add("the end");
ArrayAdapter<String> dataAdapter6 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dataAdapter6.notifyDataSetChanged();
s3.setAdapter(dataAdapter6);
}
}
First of all set adapter first to spinner and then call setOnItemSelectedListener() for it. b'z when spinner value gets initialized it even call setOnItemSelectedListener method. you can put debugger and check.
Secondly always check for which spinner or widget you are getting event, in your case for spinner you can do like this
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
Spinner clickedSpinner = (Spinner) parent;
switch (clickedSpinner.getId()) {
case R.id.spinner1:
// your code for spinner1
String str = spinner1.getSelectedItem().toString();
break;
}
}
so change your code and see its working or not. hope it will help you...
how to disable a spinner based on another spinner. For sample, when I choose first spinner value is "Yes" then the second spinner is enable also when I choose first spinner value is "No" then the second spinner is disable.
Thanks
Try this:
final Spinner sp1 = (Spinner) findViewById(R.id.spinner1);
final Spinner sp2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<String> data = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, first);
ArrayAdapter<String> data2 = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, second);
sp1.setAdapter(data);
sp2.setAdapter(data2);
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String text = sp1.getSelectedItem().toString();
if (text.equals("No")) {
sp2.setEnabled(false);
} else {
sp2.setEnabled(true);
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
http://i.stack.imgur.com/HGsTw.jpg
This is the error I get and the app just crashes and shows nothing!.
PS:When I moved those last 4 lines of code from the OnCreate method to the beginning of the OnItemSelected method I get no errors and this layout (http://i.stack.imgur.com/1DiLI.jpg) shows but no dropdown lists (I expect a list of colors when I click the arrow button next to background).
Here is my code:
public class SetLayout extends Activity implements OnItemSelectedListener {
TextView o_markertv, x_markertv, bcktv, gridtv;
String[] colors ={"Red", "Green", "Blue", "Black", "White"};
Spinner spinner1, spinner2, spinner3, spinner4;
LinearLayout bck;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setlayout);
bck= (LinearLayout) findViewById(R.id.bck);
o_markertv= (TextView) findViewById(R.id.o_markertv);
x_markertv= (TextView) findViewById(R.id.x_markertv);
bcktv= (TextView) findViewById(R.id.bcktv);
gridtv= (TextView) findViewById(R.id.gridtv);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
spinner3 = (Spinner) findViewById(R.id.spinner3);
spinner4 = (Spinner) findViewById(R.id.spinner4);
//Button BtnSubmit = (Button) findViewById(R.id.btnSubmit);
ArrayAdapter<String> adapter=new ArrayAdapter<String> (SetLayout.this,android.R.layout.simple_spinner_item,colors);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
int position= spinner1.getSelectedItemPosition();
switch(position){ //red, green blue black white
case 0:
bck.setBackgroundColor(color.red);
break;
case 1:
bck.setBackgroundColor(color.green);
break;
case 2:
bck.setBackgroundColor(color.blue);
break;
case 3:
bck.setBackgroundColor(color.black);
break;
case 4:
bck.setBackgroundColor(color.white);
break;
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
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.