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);
Related
I would like to programmatically change a name of one of my spinner item but I am not sure how I can do this.
For example given the code below
Spinner spinner = (Spinner) MenuItemCompat.getActionView(item);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner_list_item_array,android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
I would like to
Change spinner ITEM name at position 1 to "NEW TEXT"
You should change the data in R.array.spinner_list_item_array.
After you have changed the data you will need to then call adapter.notifyDataSetChanged() and then adapter.setAdapter(adapter).
CharSequence[] fiilliste = getResources().getStringArray(R.array.spinner_list_item_array);
ArrayAdapter<CharSequence> adapter =
new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item, fiilliste);
spinner.setAdapter(adapter);
fiilliste[1] = "NEW TEXT";
adapter.notifyDataSetChanged();
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);
}
I am trying to set value from array to spinner but it is working for few and not for others. Please help me out. here is my code.
edmORdelOF = (Spinner) findViewById(R.id.ed_sign_up_manf_del_of);
ArrayAdapter<CharSequence> adapterItem = ArrayAdapter.createFromResource(this,
R.array.Item, android.R.layout.simple_spinner_item);
adapterItem.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
edmORdelOF.setAdapter(adapterItem);
System.out.println(profile.getEdmORdelOF()+"....getPosition..........."+adapterItem.getPosition(profile.getEdmORdelOF()));
edmORdelOF.setSelection(adapterItem.getPosition(profile.getEdmORdelOF()));
org_type = (Spinner) findViewById(R.id.Updatesp_sign_up_manf_org_type);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Org_type, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
org_type.setAdapter(adapter);
System.out.println(profile.getD_org_type()+"....getPosition..........."+adapter.getPosition(profile.getD_org_type()));
org_type.setSelection(adapter.getPosition(profile.getD_org_type()));
country = (Spinner) findViewById(R.id.Updatesp_sign_up_country);
ArrayAdapter<CharSequence> adapterCountry = ArrayAdapter.createFromResource(this,
R.array.Country, android.R.layout.simple_spinner_item);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
country.setAdapter(adapterCountry);
System.out.println(profile.getD_country()+"....getPosition..........."+adapterCountry.getPosition(profile.getD_country()));
country.setSelection(adapterCountry.getPosition(profile.getD_country()));
here,i am getting value from bean object profile and trying to set that values to spinner but state doesn't get the value. sorry every one.i have change it
I haven't been able to find the answer to this anywhere (it's quite possible I'm not searching right). In Android I'm trying to make a single spinner in my app pull from multiple arrays depending on which radio button is selected. If radio button 1 is selected I want the spinner to use array 1, if radio button 2 is selected I want the spinner to use array 2. Here is what I have so far, but it doesn't work. Everytime I click on the tab in my app with this code my app force closes.
public class This_Activity extends Activity {
/** Called when the activity is first created. */
private RadioButton rb1;
private RadioButton rb2;
private RadioButton rb3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab_2_layout);
rb1=(RadioButton)findViewById(R.id.Radio1);
rb2=(RadioButton)findViewById(R.id.Radio2);
rb3=(RadioButton)findViewById(R.id.Radio3);}{
if(rb1.isChecked() == true){
Spinner spinner1 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
}
if(rb2.isChecked() == true){
Spinner spinner2 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array2, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
if(rb3.isChecked() == true){
Spinner spinner3 = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.Array3, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner3.setAdapter(adapter);
}
}
}
It is difficult to diagnose the problem without the error messages you are seeing. Could you edit your question and include those?
However, one thing I am noticing is that you are creating different spinners in each of those cases. Try using
Spinner spinner = (Spinner) findViewById(R.id.Spinner);
ArrayAdapter<CharSequence> adapter = null;
above your if statements. Then the body of your if statements will look like
adapter = ArrayAdapter.createFromResource(
this, R.array.Array1, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
you also probably want to hook up listeners to your radio buttons to change the spinner automatically when the user clicks on them, because as it is now this will only happen in the constructor.
rb1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// change your spinner adapter here
}
});
it is probably easier to put your radio buttons in a RadioGroup and put the OnCheckChangedListener on that, so you only need one listener for all 3 buttons.
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);