Home » Android » Android Spinner dynamically change items

Android Spinner dynamically change items

Posted by: admin June 15, 2020 Leave a comment

Questions:

I want to use a Spinner in my App. The context (items) of the Spinner are depending on the user preferences done in the Settings of the App.

I created the Spinner with no items. After loading the App the Spinner has one Entry. To get the Entries the User has to go to the menu and load an image. Based on this the items are loaded into a String Array. This String Array will be hand over to a Adapter. See Code:

List<String> als = new ArrayList<String>();
als = Arrays.asList(Data.getfloornames());
adapter = new ArrayAdapter<String>(this, R.array.flooritem, als);
adapter.notifyDataSetChanged();
floornum.setAdapter(adapter);

After this is done. The user will be returned to the menu settings. Clicking in the menu Settings the return button of the devices, result an abort of the app.

Error from Logcat is FATAL Exception: main.
05-16 15:12:43.193: E/AndroidRuntime(9128): android.content.res.Resources$NotFoundException: Resource ID #0x7f060002
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.content.res.Resources.getValue(Resources.java:1014)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.content.res.Resources.loadXmlResourceParser(Resources.java:2139)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.content.res.Resources.getLayout(Resources.java:853)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:193)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.Spinner.onMeasure(Spinner.java:439)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.View.measure(View.java:15518)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.Choreographer.doFrame(Choreographer.java:532)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.os.Handler.handleCallback(Handler.java:725)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.os.Looper.loop(Looper.java:137)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at java.lang.reflect.Method.invokeNative(Native Method)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at java.lang.reflect.Method.invoke(Method.java:511)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-16 15:12:43.193: E/AndroidRuntime(9128):     at dalvik.system.NativeStart.main(Native Method)

What am I doing wrong? Is it not possible to set the Spinner Items in the menu settings of the app?

How to&Answers:

You can try following code to add item in spinner dynamically.

Spinner spinner = (Spinner)findViewById(R.id.mySpinner);
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();

Answer:

Try to change this :

adapter.notifyDataSetChanged();
floornum.setAdapter(adapter);

into this :

floornum.setAdapter(adapter);
adapter.notifyDataSetChanged();

Answer:

See ArrayAdapter javadoc.

textViewResourceId The resource ID for a layout file containing a
TextView to use when instantiating views.