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
}
Related
I am working on a project where I have to implement a spinner. My spinner code is below:
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, CatagoryName);
// create a spinner
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// add adapter to spinner
spinner.setAdapter(stringArrayAdapter);
// create listener and add to spinner
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// put code which recognize a selected element
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Basically, I want to use my custom spinner layout at
ArrayAdapter stringArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, CatagoryName);
R.layout.simple_spinner_dropdown is not what I want. How to replace this layout with mine?
this is worked for me,
firstly define spinner items in strings.xml which in values directory as above,
<string-array name="spinner_array">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
then try above code,
Spinner customSpinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.spinner_array, R.layout.custom_spinner_dropdown);
customSpinner.setAdapter(adapter);
Try this approach:
Spinner spinner = (Spinner) findViewById(R.id.spinner);
String[] CatagoryName = {"A","B","C","D"};
ArrayAdapter<CharSequence> langAdapter = new ArrayAdapter<CharSequence>(getActivity(), R.layout.your_layout, CatagoryName);
langAdapter.setDropDownViewResource(R.layout.simple_spinner_dropdown);
spinner.setAdapter(langAdapter);
i am trying to populate two spinners based on the selection of the first so when i choose one of the options in the first spinner the second one is filled with the appropriate string array. I am kind of new in android and i can't find the mistake, i am getting an error on createFromResource inside the if loop . Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.locations, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter = ArrayAdapter.createFromResource(
this, R.array.locations, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if(position==5) {
Spinner spinner2 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.startlocations, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
else{
Spinner spinner2 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.destinations, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter);
}
Toast.makeText(getBaseContext(), parent.getItemAtPosition(position) + " is selected", Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
ArrayAdapter.createFromResource(this,
R.array.destinations, android.R.layout.simple_spinner_item);
I think this is your problem
try replacing with
ArrayAdapter.createFromResource(ActivityClassName.this,
R.array.destinations, android.R.layout.simple_spinner_item);
where "ActivityClassName" is the name of you activity
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
}
}
I'm trying to set the index of the item that needs to be selected in the spinner by default, but it always defaults to 0 (1st item)
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
SpinnerAdapter spinnerAdapter =
new ArrayAdapter<String>(activity, android.R.layout.simple_spinner_dropdown_item,
names);
int selectedIndex = actionBar.getSelectedNavigationIndex();
if (selectedIndex != targetIndex) {
actionBar.setSelectedNavigationItem(targetIndex);
}
Above if block is called always. Even after setting index 2, next time I check it returns 0.
Edit: I suspect getSelectedNavigationIndex gives index of actionBar item rather than Spinner dropdown item. If that is the case, what method sets the index of selected item inside dropdown?
Make sure you call setListNavigationCallbacks method before changing selected element. I can't see it in your example, so I think that's the problem.
Here is an example:
actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
actionBar.setListNavigationCallbacks(adapter, this);
actionBar.setSelectedNavigationItem(position);
It works in my app without any problems.
Have you tried using shared prefences to save the value of the selected spinner. I used this code to save the users selection with shared preferences so the next time they opened the app the spinner was set to the last value they chose:
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = getSpinnervalues();
ArrayAdapter<CharSequence> spinnerAdapter = new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_item, makes);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(spinnerAdapter);
int position = Utils.getIndex(getSpinnerval(), makes);
Spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
String selected = (String) parentView.getSelectedItem();
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
String[] spinnervalues = Filter.this.getSpinnerval(selected);
ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(Filter.this, android.R.layout.simple_spinner_item, models);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner.setAdapter(adapter);
}
public void onNothingSelected(AdapterView<?> parentView) {}
});
Spinner.setSelection(makeposition, true);
int position = Utils.getIndex(getSpinnerval());
if (position >= 0) {
Spinner Spinner = (Spinner) findViewById(R.id.Spinner);
Spinner.setSelection(position, true);
}
Then the get Util:
public String getSpinnerval() {
return getSharedPreferences().getString("val", "");
}
I altered the code a little so it might not be 100% right but may give you an idea.
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.