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.
Related
my problem is the following. I have 2 Spinners in one activity. I want that both spinner items should be saved in the DB. But there is a slight difference between both spinners:
Spinner 1: Is populated by data from other db-table - when it is the only spinner in the mentioned activity, it works.
Spinner 2: Has default values. I am not sure if added it correctly to my activity. Anyway, after i added it to my saving the data is not working anymore.
Please help me what am I doing wrong??
My Activity Code:
public class activity_transport extends AppCompatActivity {
private BottomNavigationViewEx bottomNavigationViewEx;
DatabaseHelper myDb;
Button btn_save;
Spinner ChooseProject,ChooseTransport;
EditText Entfernung,Price,MWST;
String selectedspinner, selectedTransport;
ImageView imageView2;
private static int PICK_IMAGE = 100;
Uri imageUri;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_transport);
myDb = new DatabaseHelper(this);
ChooseProject = (Spinner) findViewById(R.id.ChooseProject);
ChooseTransport = (Spinner) findViewById(R.id.ChooseTransport);
Entfernung = (EditText) findViewById(R.id.Entfernung);
Price = (EditText) findViewById(R.id.Preis);
MWST = (EditText) findViewById(R.id.MwSt);
btn_save=(Button) findViewById(R.id.btn_save);
//bildanzeigebereich im layout
imageView2=(ImageView) findViewById(R.id.imageView2);
loadProjectSpinnerData();
//loadTransportSpinnerData();
SaveData();
//+++++++++++++++Function to load the spinner data from SQLite database++++++++++//
private void loadProjectSpinnerData() {
// database handler
DatabaseHelper db = new DatabaseHelper (getApplicationContext());
// Spinner Drop down elements
List<String> projects = db.getAllProjects();
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseProject.setPrompt("Projekt auswählen");
// attaching data adapter to spinner
ChooseProject.setAdapter(dataAdapter);
//Choose Transport Spinner
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseProject.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinner = (Spinner) parent;
if(spinner.getId() == R.id.ChooseProject)
{
selectedspinner = (String) ChooseProject.getSelectedItem();
}
else if(spinner.getId() == R.id.ChooseTransport)
{
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
/*private void loadTransportSpinnerData() {
// Spinner method to read the on selected value
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, new String[] {
new String("Auto"),
new String("Flugzeug"),
new String("Taxi"),
new String("Bahn")});
ChooseTransport.setAdapter(spinnerArrayAdapter);
// Creating adapter for spinner
//ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, projects);
// Drop down layout style - list view with radio button
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
// attaching data adapter to spinner
ChooseTransport.setAdapter(spinnerArrayAdapter);
//Listener für den Projekt Spinner damit ich den Wert abspeichern kann
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
//selectedspinner =String.ValueOf(parent.getItemAtPosition(pos));
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
public void onNothingSelected(AdapterView<?> parent) {
}
});
}*/
//++++++++++++Save Data++++++//
public void SaveData(){
btn_save.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.createTransport(
selectedspinner,
selectedTransport,
Integer.valueOf(Price.getText().toString()),
Integer.valueOf(MWST.getText().toString()),
Integer.valueOf(Entfernung.getText().toString()),
imageViewToByte(imageView2)
);
if(isInserted=true)
Toast.makeText(activity_transport.this, "Daten gespeichert", Toast.LENGTH_LONG).show();
else
Toast.makeText(activity_transport.this, "Daten nicht gespeichert", Toast.LENGTH_LONG).show();
}
}
);
}
}
EDIT:
I tried to run the app by setting a few breakpoints.
When trying to run this line selectedTransport = (String) ChooseTransport.getSelectedItem(); the debugger posts Cannot find local variable 'ChooseTransport'
I solved the problem:
I implemented an own method for ChooseTransport- Spinner and now it works. I also figured out that the order how I add the data to the DB was not right. I changed this too.
See here my method for my second spinner:
private void loadTransportSpinnerData(){
ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.transport, android.R.layout.simple_spinner_dropdown_item);
spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ChooseTransport.setPrompt("Transport auswählen");
ChooseTransport.setAdapter(spinnerArrayAdapter);
ChooseTransport.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
selectedTransport = (String) ChooseTransport.getSelectedItem();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
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);
}
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());
}
I have a spinner which displays both options, but when I accept the female option, it still takes the answer as a male, any suggestions?
List<String> SpinnerArray = new ArrayList<String>();
SpinnerArray.add("Male");
SpinnerArray.add("Female");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, SpinnerArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner Items = (Spinner) findViewById(R.id.genderSpinner);
Items.setAdapter(adapter);
// Importing all assets like buttons, text fields
inputFullName = (EditText) findViewById(R.id.registerName);
String selected = Items.getSelectedItem().toString();
if (selected.equals("Male")) {
inputGen = "Male";
}
if (selected.equals("Female")){
inputGen = "Female";
}
Following the official guide:
public class SpinnerActivity extends Activity implements OnItemSelectedListener {
...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
You can try a switch of position and then try your same conditions.
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
}
});