ActionBar Dropdown Spinner item defaults to first item - android

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.

Related

Creating dynamic spinners and getting value of every spinner, Whenever a spinner item changed, get all spinner values

I'm developing a e-com app. I'm trying to create dynamic spinner. Spinner is dependent upon product attributes. I'm able to create spinners also mapped data on them but I want to get all spinners selected item whenever a spinner change its data so I can match to correct variant of product.
Here is my code snippet
final List<Attribute> attributes = product_.getAttributes();
for (i = 0; i < attributes.size(); i++) {
ArrayList<String> spinnerArray = (ArrayList<String>) attributes.get(i).getOptions();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
Spinner spinner = new Spinner(getActivity());
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
dynamicProductProperties.addView(spinner);
}
First, you need to set same instance of OnItemSelectListener to your all spinners. Let your activity or fragment implement OnItemSelectListener then call spinner.setOnItemSelectListener(this);
In onItemSelected method you can run this code to have selecteds.
ArrayList<String> selectedList = new ArrayList();
for (int i = 0; i < dynamicProductProperties.getChildCount(); i++) {
View view = dynamicProductProperties.getChildAt(i);
if (view instanceof Spinner){
String selected = (String) ((Spinner) view).getSelectedItem();
selectedList.add(TextUtils.isEmpty(selected) ? "" : selected);
}
}
Good luck
Emre

Default text shown on spinner but hide from drop down list - Android

I want to show first item as a default text on spinner but in the drop down list that item is not shown like there is arraylist
final ArrayList<String> spinnerArray = new ArrayList<String>();
spinnerArray.add("number");
spinnerArray.add("one");
spinnerArray.add("two");
spinnerArray.add("three");
The number is always shown as a default text on spinner but after clicking on spinner there is one, two, three is shown in drop down list. when we select any number one, two three the default text number is not changed. How can i do it?
ArrayAdapter<String> adapte = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, spinnerArray);
adapte.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin.setAdapter(adapte);
spin.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> parent, View arg1,int pos, long arg3)
{
}
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Try this, it show the default as Number..
May be this help you
String[] priceString = { "Number", "one, "two"};
Spinner priceSpinner = (Spinner) findViewById(R.id.price_spinner);
ArrayAdapter priceAdapter = new ArrayAdapter( this.getActivity(), android.R.layout.simple_spinner_item,priceString); priceSpinner.setAdapter(priceAdapter);

Selection on second spinner not working

I am developing a page with 2 spinners, where the first spinner has some category populated from DB.
Based On the value selected from spinner1, the selectedCategoryName is passed in the method setReportNamesContent().
I am also getting the values populated in the second spinner, but I am unable to do the selection in the 2nd spinner.
My idea is to pass both these selected values on click of a button, but always the 1st value from the 2nd spinner is getting passed on click of the button.
protected void setReportCategory(String loginName) {
map = DBManager.getInstance().reportCategoryListByLoggedInUser(loginName);
reportCategoryList = new ArrayList<String>(map.keySet());
Collections.sort(reportCategoryList);
reportCategorySpinnner = (Spinner) findViewById(R.id.ReportCategoryList);
reportCategorySpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportCategoryList);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportCategorySpinnner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch(parent.getId()){
case R.id.ReportCategoryList:
selectedCategoryName = parent.getItemAtPosition(position).toString();
break;
case R.id.ReportNameList:
selectedReportName = parent.getItemAtPosition(position).toString();
break;
}
if(!isEmptyOrNull(selectedCategoryName))
setReportNamesContent(selectedCategoryName);
}
private void setReportNamesContent(String selectedCategoryName) {
reportNameList = map.get(selectedCategoryName);
Collections.sort(reportNameList);
reportNameSpinnner = (Spinner) findViewById(R.id.ReportNameList);
reportNameSpinnner.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, reportNameList);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
reportNameSpinnner.setAdapter(dataAdapter1);
}
I had already reffered various question in this site and tried all of them and are not working. Please advice for a working answer

chanding contents of spinners run-time (android), tried and failed

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
}
}

How to add more items at run time in spinner

if we has a some items in the spinner means (like below) how shall we add extra items while run time
here is code:
String[] Items = {
"Alarm",
"Office",
"Meeting",
"Party",
"Lunch",
"Breakfast",
"Supper",
"Home",
"Private",
"Outdoor",
"Family",
"Friends",
"Others"
};
Spinner s1;
and
s1 = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
this,android.R.layout.simple_spinner_item,Items);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
s1.setAdapter(adapter);
s1.setOnItemSelectedListener(new OnItemSelectedListener()
{
public void onItemSelected(AdapterView<?> arg0,
View arg1, int arg2, long arg3)
{
int index = s1.getSelectedItemPosition();
Toast.makeText(getBaseContext(),
"You have selected item : " + Items[index],
Toast.LENGTH_SHORT).show();
if (index==12)
{
EditText edit = (EditText) findViewById(R.id.edittext);
Button add=(Button) findViewById(R.id.add);
edit.setVisibility(View.VISIBLE);
add.setVisibility(View.VISIBLE);
}
else
{
EditText edit = (EditText) findViewById(R.id.edittext);
Button add=(Button) findViewById(R.id.add);
edit.setVisibility(View.GONE);
add.setVisibility(View.GONE);
}
}
public void onNothingSelected(AdapterView<?> arg0) {}
});
}
In it if select the "other" means we ve to add extra items in spinner list..
thank you,
I hope you can detect the case when the user has selected "others"
in that case, add the other values in your Items array and call the function adapter.notifyDataSetChanged() it should accommodate the changes and you should see the new values in the spinner.
try this out and revert.
You can create a BaseAdapter, fill it in runtime and assign the adapter to the spinner.
Following works for me:
/* Read values from resource into an array */
String[] strColorValues = getResources().getStringArray(R.array.colors);
ArrayList<String> list = new ArrayList<String>();
for (int i = 0; i < strColorValues.length; i++) {
list.add(strColorValues[i]);
}
ArrayAdapter adapterColors = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);
adapterColors.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerColors.setAdapter(adapterPermissionLevels);
spinnerColors.setOnItemSelectedListener(this);
/* Remove first element from the adapter and notify dataset changed. */
String item = spinnerColors.getItemAtPosition(0).toString();
adapterColors.remove(item);
adapterColors.notifyDataSetChanged();

Categories

Resources