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
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 set and get value to a spinner for item dynamically ?
any ideas ?
I just need help with the Spinner behavior right now, the rest should be quite easy.
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, android.R.id.text1);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
spinnerAdapter.add("value");
spinnerAdapter.notifyDataSetChanged();
spinner.setSelection(0);
String text = spinner.getSelectedItem().toString();
1
2
3
XML file:
<Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Java file:
public class SpinnerExample extends Activity {
private String[] arraySpinner;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.arraySpinner = new String[] {
"1", "2", "3", "4", "5"
};
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, arraySpinner);
s.setAdapter(adapter);
}
// To get value from spenner
spinner.setOnItemSelectedListener(newAdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView parent, View view, int pos, long id) {
Object item = parent.getItemAtPosition(pos);
}
public void onNothingSelected(AdapterView parent) {
}
});
}
Try this this will help you.
Spinner mySpinner= (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item);
mySpinner.setAdapter(myAdapter);
If you want to add the elements dynamically, you can by doing this:
myAdapter.add("newelement");
myAdapter.notifyDataSetChanged();
Spinner spinn = findViewById(R.id.socialmedia_spinner_adsocmeda);
ArrayList<String> fam = new ArrayList<>();
fam.add("INSTAGRAM");
fam.add("FACEBOOK");
fam.add("GOODWALL");
fam.add("TWITTER");
fam.add("YOUTUBE");
fam.add("LINKEDIN");
fam.add("SNAPCHAT");
ArrayAdapter<String> myAdapter= new ArrayAdapter<String> (ProfileActivity.this,
android.R.layout.simple_list_item_1, fam.toArray(new String[0]));
spinn.setAdapter(myAdapter);
That's a way we can add data to spinner.
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
}
http://developer.android.com/resources/tutorials/views/hello-spinner.html
I use this link as the base..
I have 3 spinners, first spinner for country , second for states and third for cities.
I had already seen the links on the stackoverflow , but it is not that much proper.
my code is as follows
public class SpinnerActivity extends Activity
{
String state="";
ArrayAdapter adapter1,adapter2;
Spinner spinner1,spinner2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner2 = (Spinner) findViewById(R.id.spinner2);
adapter1 = ArrayAdapter.createFromResource(
this, R.array.states_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MyOnItemSelectedListener());
if(state.equalsIgnoreCase("Gujarat"))
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
if(state == "Rajasthan")
{
adapter2 = ArrayAdapter.createFromResource(this, R.array.rajasthan_array, >android.R.layout.simple_spinner_item);
}
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner2.setAdapter(adapter2);
spinner2.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener
{
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
state=parent.getItemAtPosition(pos).toString();
Toast.makeText(parent.getContext(), "The state is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView parent)
{
// Do nothing.
}
}
}
the bug I found is , I have created all he thing in the onCreate() method..
but I do not find the alternate thing that can I do ...
I don't know why this not work ..
please help if possible
thanks...
In onSelectItem fill second spinner and so on.
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.