Android Spinner - How to choose the default list for none

I have an Android form that needs to be updated on certain selections. The form currently consists of 2 spinners (A and B). Spinner B is not created until Spinner is selected. After the selection is made, B will be displayed in the view, and the content will be dynamically populated based on selection A. Here is my code:

public class MyForm extends Activity { private final int SEL_ACTIVATE = 0; private final int SEL_DEACTIVATE = 1; private static final String[] actionList = {"Activate", "Deactivate" }; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); table = (TableLayout) findViewById(R.id.table); showListA(table); } public void showListA(View v) { rowAction = new TableRow(this); Spinner spinner = new Spinner(this); spinner.setPrompt("Select..."); spinner.setOnItemSelectedListener( new OnItemSelectedListener() { public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { switch (position) { case SEL_ACTIVATE: case SEL_DEACTIVATE: showListB(v); break; } } public void onNothingSelected(AdapterView<?> arg0) { // TODO Auto-generated method stub } }); ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item, actionList); adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line); spinner.setAdapter(adapter); rowAction.addView(tvAction); rowAction.addView(spinner); table.addView(rowAction, new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT)); } ... } 

This code is working correctly. If Activate or Deactivate is selected in the list, showListB() is showListB() , which is very similar to showListA() in the way it creates a new line containing Label and Spinner.

The problem is that by default, “Activation” is displayed in Spinner, which runs showListB() and immediately from the quarry, the second part of the form is created based on the “Activate” option. The only workaround I can come up with is to add a third field to Spinner as follows:

 private static final String[] actionList = {"None", "Activate", "Deactivate" }; ... switch (position) { case SEL_NONE: break; case SEL_ACTIVATE: case SEL_DEACTIVATE: showListB(v); break; } 

This works ... but I do not want to have the third option on the list. I just want it to be empty by default or to show some kind of "invitation" text, which is not an option in the list after clicking it. Is it possible?

thanks

EDIT:

xml content:

 <Spinner android:id="@+id/spinnerAction" android:layout_width="wrap_content" android:layout_height="wrap_content"/> 
+6
android spinner listeners
source share
5 answers

My data-sizes.xml:

 <?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="chunks"> <item>2</item> <item>4</item> <item>8</item> <item>16</item> <item>32</item> </string-array> </resources> 

In main.xml:

 <Spinner android:id="@+id/spinnerSize" android:layout_marginLeft="50px" android:layout_width="fill_parent" android:drawSelectorOnTop="true" android:layout_marginTop="5dip" android:prompt="@string/SelectSize" android:layout_marginRight="30px" android:layout_height="35px" /> 

In Java code:

 Spinner spinnerSize; ArrayAdapter adapter; ... spinnerSize = (Spinner)findViewById(R.id.spinnerSize); adapter = ArrayAdapter.createFromResource(this, R.array.chunks, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinnerSize.setAdapter(adapter); spinnerSize.setOnItemSelectedListener(new MyOnItemSelectedListener()); ... class MyOnItemSelectedListener implements OnItemSelectedListener { public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) { chunkSize = new Integer(parent.getItemAtPosition(pos).toString()).intValue(); } public void onNothingSelected(AdapterView<?> parent) { // Dummy } } 

So, although I can see 2 as my first default element, nothing happens unless the user actually selects it.

Hope this helps!

+5
source share

If you want a spinnerAdapter decorator, add the default value automatically:

 protected class SpinnerAdapterWithNoValue implements SpinnerAdapter { private SpinnerAdapter _current; private final static String defaultValue = "Choisir"; public SpinnerAdapterWithNoValue(SpinnerAdapter base) { _current = base; } @Override public int getCount() { return _current.getCount() + 1; } @Override public Object getItem(int position) { if (position == 0 || position == -1) { return null; } return _current.getItem(position - 1); } @Override public long getItemId(int position) { if (position == 0 || position == -1) { return -1; } return _current.getItemId(position - 1); } @Override public int getItemViewType(int position) { if (position == 0 || position == -1) { return -1; } return _current.getItemViewType(position - 1); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (position == 0 || position == -1) { final TextView v = (TextView) ((LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_text, parent, false); v.setText(defaultValue); return v; } return _current.getView(position - 1, convertView, parent); } @Override public int getViewTypeCount() { return _current.getViewTypeCount(); } @Override public boolean hasStableIds() { return _current.hasStableIds(); } @Override public boolean isEmpty() { return _current.isEmpty(); } @Override public void registerDataSetObserver(DataSetObserver observer) { _current.registerDataSetObserver(observer); } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub _current.unregisterDataSetObserver(observer); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if (position == 0 || position == -1) { CheckedTextView v = (CheckedTextView) ((LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE)).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); v.setText(defaultValue); return v; } return _current.getDropDownView(position - 1, convertView, parent); } }
protected class SpinnerAdapterWithNoValue implements SpinnerAdapter { private SpinnerAdapter _current; private final static String defaultValue = "Choisir"; public SpinnerAdapterWithNoValue(SpinnerAdapter base) { _current = base; } @Override public int getCount() { return _current.getCount() + 1; } @Override public Object getItem(int position) { if (position == 0 || position == -1) { return null; } return _current.getItem(position - 1); } @Override public long getItemId(int position) { if (position == 0 || position == -1) { return -1; } return _current.getItemId(position - 1); } @Override public int getItemViewType(int position) { if (position == 0 || position == -1) { return -1; } return _current.getItemViewType(position - 1); } @Override public View getView(int position, View convertView, ViewGroup parent) { if (position == 0 || position == -1) { final TextView v = (TextView) ((LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_text, parent, false); v.setText(defaultValue); return v; } return _current.getView(position - 1, convertView, parent); } @Override public int getViewTypeCount() { return _current.getViewTypeCount(); } @Override public boolean hasStableIds() { return _current.hasStableIds(); } @Override public boolean isEmpty() { return _current.isEmpty(); } @Override public void registerDataSetObserver(DataSetObserver observer) { _current.registerDataSetObserver(observer); } @Override public void unregisterDataSetObserver(DataSetObserver observer) { // TODO Auto-generated method stub _current.unregisterDataSetObserver(observer); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { // TODO Auto-generated method stub if (position == 0 || position == -1) { CheckedTextView v = (CheckedTextView) ((LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE)).inflate(android.R.layout.simple_spinner_dropdown_item, parent, false); v.setText(defaultValue); return v; } return _current.getDropDownView(position - 1, convertView, parent); } } 

Then you can create your own counter using this decorator:

 public class SpinnerWithNoValue extends Spinner { public SpinnerWithNoValue(Context context) { super(context); } public SpinnerWithNoValue(Context context, AttributeSet attrs) { super(context, attrs); } public SpinnerWithNoValue(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setAdapter(SpinnerAdapter orig) { final SpinnerAdapter adapter = new SpinnerAdapterWithNoValue(orig); super.setAdapter(adapter); try { final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class); m.setAccessible(true); m.invoke(this, -1); final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class); n.setAccessible(true); n.invoke(this, -1); } catch (Exception e) { throw new RuntimeException(e); } } /* * getSelectedItem renvoi null si la valeur par defaut est séléctionnée * * @see android.widget.AdapterView#getSelectedItem() */ @Override public Object getSelectedItem() { return super.getSelectedItem(); } }
public class SpinnerWithNoValue extends Spinner { public SpinnerWithNoValue(Context context) { super(context); } public SpinnerWithNoValue(Context context, AttributeSet attrs) { super(context, attrs); } public SpinnerWithNoValue(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } @Override public void setAdapter(SpinnerAdapter orig) { final SpinnerAdapter adapter = new SpinnerAdapterWithNoValue(orig); super.setAdapter(adapter); try { final Method m = AdapterView.class.getDeclaredMethod("setNextSelectedPositionInt", int.class); m.setAccessible(true); m.invoke(this, -1); final Method n = AdapterView.class.getDeclaredMethod("setSelectedPositionInt", int.class); n.setAccessible(true); n.invoke(this, -1); } catch (Exception e) { throw new RuntimeException(e); } } /* * getSelectedItem renvoi null si la valeur par defaut est séléctionnée * * @see android.widget.AdapterView#getSelectedItem() */ @Override public Object getSelectedItem() { return super.getSelectedItem(); } } 

You just need to change the spinner declaration in your XML layout:

 com.myproject.SpinnerWithNoValue
com.myproject.SpinnerWithNoValue 

If you want, you can change the code to set the default text in your counter tag.

+2
source share

Spinner will always have a choice. What you can do is make Spinner display something like “Select” or “Select Parameter” ...

To do this, you can make your list options

 actionList = {"Select", "Desactivate" } 

and

 public void onItemSelected(AdapterView<?> arg0, View arg1, int position, long id) { actionList[0] = "Activate"; ... } 

or you can do something more complicated, for example, redefine Spinner View or instead put a button on which there is nothing, and when it clicks, you will create your own counter.

+1
source share

Try setting this in xml with the prompt attribute:

 android:prompt="@string/prompt" 

It will also fill the top of the spinner dialog box.

I skipped this in the documentation. Cannot directly apply the invitation. The prompt attribute must refer to another source. Try referencing the string value.

Android Documentation:

 android:prompt Since: API Level The prompt to display when the spinner dialog is shown. Must be a reference to another resource, in the form "@[+][package:]type:name" or to a theme attribute in the form "?[package:][type:]name". This corresponds to the global attribute resource symbol prompt. 
-one
source share

I am just a beginner Android app developer and have encountered a similar problem; I was able to deal with it using a simple solution - it worked for me, I hope this is for you too:

public class AdmissionActivity extends Activity implements OnItemSelectedListener {

 int i; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); i=0; ......... } 

........ .....

 public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){ if(i!=0){ //do wat you want; } else i=1; } //initially the activity will be loaded and nothing will happen (since // i=0); and then appropriate action will be taken since i would hav been 

// changed to sumtng else

-one
source share

All Articles