Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Spinner dynamically change items

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?

like image 408
Irgendw Pointer Avatar asked May 16 '13 13:05

Irgendw Pointer


People also ask

How to use spinner in Android app explain with an example?

Example to demonstrate the SpinnerUse ArrayAdapter to store the courses list. Create a single MainActivity that contains the spinner and on clicking any item of spinner Toast with that course name will be shown. Creating the activities: There will be one activity and hence one XML file for MainActivity. activity_main.


1 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();
like image 181
Sandeep Avatar answered Oct 28 '22 07:10

Sandeep