In my application there are two spinners. The content of second spinner depends upon first spinner. So, I want to fill the second one from resource R.array.
if(state.getSelectedItemPosition()==2)
{
adapter = new ArrayAdapter<String> (MainActivity.this,android.R.layout.simple_spinner_dropdown_item,R.array.Panjab);
city.setAdapter(adapter);
}
Related
String[] s={"1","2","3","4"};
ArrayAdapter<String> adp=new ArrayAdapter<String>(getApplicationContext(),android.R.layout.simple_spinner_dropdown_item,s);
spin.setAdapter(adp);
Now I want to get "4" at zero position of Spinner.
AFAIK, you can't change the position of the items inside a Spinner after you have set its Adapter.
If you want 4 to be the first item, you have to order your list accordingly:
String[] s={"4","1","2","3"};
ArrayAdapter<String> adp = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_dropdown_item, s);
spin.setAdapter(adp);
I need to put multiple spinners into my activity. Number of them would be defined dynamically, it would be 2-7 items.
At the moment I have something like:
-- clip:
Spinner spinnerOne = (Spinner) findViewById(R.id.spinnerBrowse);
spinnerOne.setAdapter(new Adapter(Browse.this, R.layout.browse_spinner_rows, picsIds));
spinnerOne.setOnItemSelectedListener(this);
Spinner spinnerTwo = (Spinner) findViewById(R.id.spinnerBrowse);
spinnerTwo.setAdapter(new Adapter(Browse.this, R.layout.browse_spinner_rows, picsIds));
spinnerTwo.setOnItemSelectedListener(this);
-- clap.
Contents of these spinners is the same, they only vary by names. Is it somehow possible to iterate by these names, like putting names into array { "SpinnerOne", "SpinnerTwo", "SpinnerThree", ... } and just generate number of needed spinner items in a loop?
Sure define a container on your layout and then add spinners dinamically
int numOfSpinners;
LinearLayout container = (LinearLayout)findViewById(R.id.container);
for(int i=0;i<numOfSpinners;i++)
{
Spinner spinner = new Spinner(this);
spinner.setAdapter(new Adapter(Browse.this, R.layout.browse_spinner_rows, getPicsIds(i)));
spinner.setOnItemSelectedListener(this);
container.addView(spinner);
}
where getPicsIds() fetches the right items for each iteration
I have 2 spinners, the 2 spinners should have different selected item.
this is the addItemsOnSpinner methode, the 2 spinners uses the same array of strings.
public void addItemsOnSpinner(Spinner spinner,String[] names) {
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, names);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
}
How can I remove the item that I selected it in spinner 1 from spinner 2 ?
You can save an original of String[] names, then when selection occurs on first spinner, recreate the second spinner data adapter using the original list but removing the element you selected in spinner one, jus an idea i havanet touched android for a while
I'm trying to create spinner which should not have any select but instead of it, it should show Blank, after clicking that items can be selected.
Here is my code, please help.
urineGlucoseSpinner = (Spinner) view.findViewById(R.id.spnner_urine_glucose);
ArrayList<String> ugList = new ArrayList<String>();
ugList.add(0,"");
ugList.add("1.5");
ugList.add("5.5");
ugList.add("0.8");
ugList.add("9.5");
ugList.add("12.0");
//ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_spinner_item, ugList);
ArrayAdapter<String> urineGlucoseAdapter = new ArrayAdapter<String>(getActivity(),R.layout.custom_spinner_text, ugList);
urineGlucoseAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
urineGlucoseSpinner.setAdapter(urineGlucoseAdapter);
urineGlucoseSpinner.setSelection(0);
urineGlucoseSpinner.setOnItemSelectedListener(new OnUGItemSelected());
By default spinner takes array 0th element if u not selecting any one..u have to make object of ArrayList and for 0th element u have to put "" (null Sting) inside semicolon and make it as 0th element...i think this is the only solution for your question..
ArrayList<String> ugList = new ArrayList<String>();
ugList.add("");
I can see two ways to do this.
1) Add the blank line to your data at position 0, and then create a custom spinner adapter and override the getView method and in it use an if to set the 0 position view to GONE (thus getting rid of the blank line in the listing).
2) An alternative might be setting an empty EditText in your form, and when it gains focus pop a listview in a dialog with your possible choices.
I have a spinner set up like this:
ArrayAdapter<String> states = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.stateabbrev));
states.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
state.setAdapter(states);
As you can see, the source is an array.xml file.
I want to know how to populate it if I know the array value. For instance, I am retrieving information from my database and the user is from "KY" so I have a string "KY" and I want the spinner selection to be on "KY"
at first we should get position of "KY"
int position = states.getPosition("KY");
after that, select in spinner with position
state.setSelection(position);