I am trying to connect 2 spinners together. Meaning, the items inside 2nd spinner will depend on whatever item is chosen for the 1st spinner.
This is the code inside the main java file.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.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);
}
This is the code inside MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext()), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
So the onItemSelected function will print out the item that was chosen in 1st spinner on the screen. However, I can't figure out how to create the 2nd spinner fully based on the value inside 1st spinner.
I know that there should be something needed to be done inside onItemSelected, but I just can't figure it out since I am a newbie in Java Android.
Can you guys assist me on this?
Thank you.
You should just do something very similar to what you do in onCreate, only with the other spinner. For example:
List<String> values = findValuesBySelection(parent.getItemAtPosition(pos).toString());
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, values, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
Related
firstly I'm a newbie to using this program so any help is appreciated. I need spinner 1 to show 7 cities the user can choose from and spinner 2 to show the same 7 cities that the user can select.
I have some code below but my tutor says it's wrong and it won't work for the second spinners I tried to find a way but can seem to do so
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.Cities, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String text = parent.getItemAtPosition(position).toString();
Toast.makeText(parent.getContext(), text, Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
}
spinner 1 is meant to be the start destination and spinner 2 is meant to be the destination they are going to. once the user selects the options it tells them the distance from spinner 1 to spinner 2.( the distances are already given to us in a table)
Your code contains only one spinner.
Spinner spinner = findViewById(R.id.spinner1);
Define one more spinner in XML and assign the same adapter to new spinner and set a separate OnItemSelectedListener each spinner.
Get the values(city names) from each spinner and calculate the distance.
I want to make two spinners, the first spinner have the list of states and second spinner contains the list of cities . If i select the particular state from the first spinner then next spinner must show only the cities on the selected state only .
My android code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner state = (Spinner) findViewById(R.id.spinnerState);
ArrayAdapter<CharSequence> stateadapter = ArrayAdapter.createFromResource(
this, R.array.item_state, android.R.layout.simple_spinner_item);
stateadapter.setDropDownViewResource(R.layout.spinner_layout);
state.setAdapter(stateadapter);
Spinner city = (Spinner) findViewById(R.id.spinnerCity);
ArrayAdapter<CharSequence> cityadapter = ArrayAdapter.createFromResource(
this, R.array.item_city, android.R.layout.simple_spinner_item);
cityadapter.setDropDownViewResource(R.layout.spinner_layout);
city.setAdapter(cityadapter);
}}
I have created all my for the state and cities.
There are lots of ways you can achieve this, for example:
ArrayAdapter<CharSequence> stateadapter;
switch(state)
{
case "Florida":
{
stateadapter = ArrayAdapter.createFromResource(this, R.array.cities_florida, android.R.layout.simple_spinner_item);
} break;
}
(kinda hardcoded)
The most optimal solution is to define it on an xml file (maybe you can get this somewhere on the internet) and write a class that reads the file and return all the cities on the selected state.
read:
https://developer.android.com/training/basics/network-ops/xml.html
Add a listener to the Spinner state
state.setOnItemSelectedListener(this);
Implement the listener:
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedState = state.getSelectedItem().toString();
List<String> citiesInState = new ArrayList<>();
// add all cities in selectedState to this list using citiesInState.add();
// this will depend upon how you are storing the cities and states
ArrayAdapter<String> cityDataAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_spinner_item, citiesInState);
cityDataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
city.setAdapter(cityDataAdapter);
}
Im filling two spinners with text arrays from an xml resource file. depending on the selection in one spinner i want to fill the other with one of two different arrays from the xml resource file. im having trouble doing this in mono for android and cant find any examples online. please help
try this...
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
if(pos==0){
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arrayone, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
else{
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.arraytwo, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
your suggestion was a big help. With some changes, here is what worked for me in mono for android:
I have a spinner name spin3. You can select opt1 or opt2. There is a string array in the resources named opt1 and another named opt2. Then a second spinner (spinner4) changes its content to opt1 or opt2 based on what is selected in spin3.
public void spin3_onItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
Spinner spin = (Spinner)sender;
string pos = spin.SelectedItem.ToString();
if(pos=="opt1")
{
Spinner spinner = (Spinner)FindViewById(Resource.Id.spinner4);
ArrayAdapter adapter = ArrayAdapter.CreateFromResource this, Resource.Array.opt1, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
else
{
Spinner spinner = (Spinner) FindViewById(Resource.Id.spinner4);
ArrayAdapter adapter = ArrayAdapter.CreateFromResource(this, Resource.Array.opt2, Android.Resource.Layout.SimpleSpinnerItem);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
spinner.Adapter = adapter;
}
}
Then to call the event use this
spin3.ItemSelected += new EventHandler<AdapterView.ItemSelectedEventArgs>(spin3_onItemSelected);
I'd like to show the text containing result of addition of two values: one from onespinner selected item, and another from twospinner selected item. But eclipse shows an error in line
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
What's the matter? Full code goes below.
public class photographer extends Activity implements OnItemSelectedListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner onespinner = (Spinner) findViewById(R.id.spinner1);
ArrayAdapter<CharSequence> unitadapter = ArrayAdapter.createFromResource(
this, R.array.onespinner, android.R.layout.simple_spinner_item);
unitadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
onespinner.setAdapter(unitadapter);
onespinner.setOnItemSelectedListener(this);
Spinner twospinner = (Spinner) findViewById(R.id.spinner2);
ArrayAdapter<CharSequence> courseadapter = ArrayAdapter.createFromResource(
this, R.array.twospinner, android.R.layout.simple_spinner_item);
courseadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
twospinner.setAdapter(courseadapter);
twospinner.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
TextView text = (TextView)findViewById(R.id.result);
text.setText(onespinner.getSelectedItem + twospinner.getSelectedItem);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
getSelectedItem is a method, but you are referencing it like an instance variable. You need to change your code to:
text.setText(onespinner.getSelectedItem() + twospinner.getSelectedItem());
You haven't posted the exact error, but I'm guessing that your problem is that you are trying to make a reference to onespinner and twospinner in onItemSelected and those two objects are not in the scope of that function; they are declared in onCreate.
Now, the View view argument of onItemSelected is the spinner that was clicked, but you need a reference to both spinners (not just the one that was selected). Easiest way to do this is to globally declare oneSpinner and twoSpinner and that should solve your problem.
EDIT: Also what Ted Hopp said.
I was using a spinner in a TableRow and it was working perfectly well except that I didn't like spinner icon is stretching out according to the selected item. I tried to delete Spinner section in xml and create it on my code.
To create a spinner in OnCreate():
selectArea = /*(Spinner)this.findViewById(R.id.spinner);*/new Spinner(this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());
selectArea.setSelection(prefInt);
To handle a selection event:
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Log.d(TAG, "onItemSelected() " + id);
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
It worked almost perfect. TableRow show no spinner icon, on touching TableRow, it pops up items to be selected. My only problem is that, on selecting one tiem, I never get my onItemSelected() called..
What could be wrong?
please change this line
spinner.setAdapter(adapter);
into the line
selectedArea.setAdapter(adapter);
see i have created spinner here
Spinner selectArea= new Spinner(Activity.this);
String[] ss = getResources().getStringArray(R.array.countries);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, ss);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
selectArea.setAdapter(adapter);
linearLayout1.addView(selectArea);//to add your spinner
selectArea.setOnItemSelectedListener(new MyOnItemSelectedListener());