tag:blogger.com,1999:blog-8214401912480503366.post8344567110710742275..comments2023-08-10T13:35:15.093+02:00Comments on My life with Android :-): Spinner and its data behindGabor Pallerhttp://www.blogger.com/profile/14307475522972458932noreply@blogger.comBlogger19125tag:blogger.com,1999:blog-8214401912480503366.post-90109058652786144702016-04-16T01:02:50.052+02:002016-04-16T01:02:50.052+02:00Thank you very much, it is simple and very useful!...Thank you very much, it is simple and very useful!<br /><br />If someone need, as I did, to select programmatically the value of the spinner, not based on the index or value but based on the ID (from a database for example) you can do this (I am a beginner, not sure is the optimal way but it works):<br /><br />Suppose you have on your database a table with 2 columns:<br />ID Language<br />11 Italian<br />12 Portuguese<br />13 Spanish<br />and you need to select the language from the spinner with ID = 12:<br /><br />add:<br />private MyData lista_limbi[] = new MyData[0];<br />on the declarations of the class<br /><br />change old values with:<br />lista_limbi = Arrays.copyOf(lista_limbi, lista_limbi.length + 4);<br />lista_limbi[0] = new MyData( "","" );<br />lista_limbi[1] = new MyData( "Italian", "11" );<br />lista_limbi[2] = new MyData( "Portuguese", "12" );<br />lista_limbi[3] = new MyData( "Spanish", "13" );<br /><br />change<br />if (spinner.getItemAtPosition(i).toString().equalsIgnoreCase(myString))<br />whith<br />if (lista_limbi[i].getValue() == myString)<br /><br />and then from the main program change the selected item:<br />Spinner mySpinner = (Spinner)findViewById(R.id.spinner_limbi);<br />mySpinner.setSelection(getIndex(mySpinner, "12"));<br /><br />=> it will be selected the line with 'Portuguese'Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-64142844351464627042012-04-19T14:54:19.974+02:002012-04-19T14:54:19.974+02:00Thank you for your share, helpe me a lot.Thank you for your share, helpe me a lot.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-74457596092772532742012-01-24T06:26:48.074+01:002012-01-24T06:26:48.074+01:00Hey. this works good.. but is their any way to cre...Hey. this works good.. but is their any way to create multi-level spinner in android.... i.e. options within options in androidcErEbRaL aSsAsSiNhttps://www.blogger.com/profile/04810170507946163015noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-70639873302262893562011-11-06T21:06:59.349+01:002011-11-06T21:06:59.349+01:00Anonymous, toString() is only relevant if you use ...Anonymous, toString() is only relevant if you use ArrayAdapter. There are a bunch of other Adapters available and you can easily write your own.Gabor Pallerhttps://www.blogger.com/profile/14307475522972458932noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-78405635184518927782011-11-06T14:02:54.122+01:002011-11-06T14:02:54.122+01:00there is a way to change the renderer of the spinn...there is a way to change the renderer of the spinner as it is in Java Swing? Do I necessary have to take care of the "toString" method or there is a more flexible way?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-72101011077465934322011-09-15T09:08:36.453+02:002011-09-15T09:08:36.453+02:00After selecting spinner item entering into the nex...After selecting spinner item entering into the next view but when we write onitem click listener for this first time when we run the app it'll automatically goes to the next view. <br /><br />but it needs to go after we selecting the item how can i solve this problem.harishhttps://www.blogger.com/profile/12319886811610207858noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-47149241792885008132011-09-10T18:12:31.120+02:002011-09-10T18:12:31.120+02:00Juan Pablo, getPosition's prototype is exactly...Juan Pablo, getPosition's prototype is exactly:<br />int getPosition(T item)<br /><br />This means that if you template ArrayAdapter, then you have to pass a MyData instance as getPosition input argument. That's not enough, you have to implement an equals() method in MyData. <a href="http://www.artima.com/lejava/articles/equality.html" rel="nofollow">Read this tutorial about the equals() method.</a>.<br /><br />When you implement equals() you have to decide when you consider two MyData objects equal. If I understand correctly, you want to search by database IDs so you should consider two MyData instances equals if their database ID fields are equal.Gabor Pallerhttps://www.blogger.com/profile/14307475522972458932noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-85773642963123655422011-09-09T23:30:34.908+02:002011-09-09T23:30:34.908+02:00First Thanks a lot for this piece of code.
I have...First Thanks a lot for this piece of code.<br /><br />I have a question I would be very happy if someone can lead me to some directions. I am not a hardcore developer more of a designer.<br /><br />The Q: I am working with a database and I have a spinner with some clients name; I show the client name as the "value" and the client database ID as the "key". I then store the "key" into some other table in the database in case the user changes the client's name.<br /><br />When the user wants to modify the data I need to get from the number Id of the client the proper position in the spinner.<br /><br />I was doing it fine with getPosition(String) but with this new Adapter you have I cannot get the position I always get a -1. I can not return to the normal ArrayAdapter because I need to be able to store the "key" and the "value" provided by your class.<br /><br />What should i do?<br />Do I make myself clear. my english is not so good.<br /><br />Thank you very muchJuan Pablo Solanohttps://www.blogger.com/profile/13160015126381245608noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-24651731006725323102011-08-17T11:08:39.669+02:002011-08-17T11:08:39.669+02:00hi,
this is very good tutorial for spinner , who h...hi,<br />this is very good tutorial for spinner , who have created his own data . this very much useful for me<br />very good.RAJAREDDYhttps://www.blogger.com/profile/11058927628309707165noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-40686846699913065622011-07-06T03:14:28.896+02:002011-07-06T03:14:28.896+02:00Exactly what I was looking for. Thanks for posting...Exactly what I was looking for. Thanks for posting.bruno imbrizihttp://www.brunoimbrizi.comnoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-48956630062650099342011-05-16T14:35:24.599+02:002011-05-16T14:35:24.599+02:00To pick up where agitcraft left off, I think what ...To pick up where agitcraft left off, I think what we're looking for is a way to define such an array in the resources xml files (same as you would do with a string array for a "regular" spinner). I mean an array where the element type is MyData.<br />Does anyone have such an example?orenhttps://www.blogger.com/profile/03399493698903326357noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-39005285834858933532010-10-26T14:25:23.218+02:002010-10-26T14:25:23.218+02:00agitcraft, I am not sure I understand your questio...agitcraft, I am not sure I understand your question. If you want to populate the spinner from external XML, all you have to do is to create the data items behind the spinner from the XML file. Android has DOM, SAX and Pull parsers built in from API version 1 so this should not be a problem.Gabor Pallerhttps://www.blogger.com/profile/14307475522972458932noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-1323862830425827462010-10-26T01:05:25.553+02:002010-10-26T01:05:25.553+02:00Thanks for the explanation. This is all very strai...Thanks for the explanation. This is all very straight forward for these purposes, but I wonder if there isn't a way to accomplish this while still populating the spinner with items from an external XML? This question probably has more to do with basic java literacy than with the way a spinner works...Unknownhttps://www.blogger.com/profile/01253480878861832241noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-86734839558633288132010-08-28T01:24:35.257+02:002010-08-28T01:24:35.257+02:00Thanks for the help!
Just to finalize what Anonym...Thanks for the help!<br /><br />Just to finalize what Anonymous (SunD) wrote in the first message, the proper way to get the data in the event listener would be:<br /><br />SpinnerData d = (MyData)parent.getAdapter().getItem(position);SkyDivernoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-48240026177602349682010-07-14T21:51:33.272+02:002010-07-14T21:51:33.272+02:00This has helped me loads in my latest app createio...This has helped me loads in my latest app createion.<br /><br />How can I get the spinner to remember what it was set to once the app ic closed?<br /><br />can I and something to my onDestroy()<br /><br />Many thanks<br /><br />MeblinMeblinhttps://www.blogger.com/profile/06580611766592608506noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-35905355218442863462010-03-12T13:43:04.204+01:002010-03-12T13:43:04.204+01:00setPrompt(CharSequence prompt) i think u may use i...<b>setPrompt(CharSequence prompt)</b> i think u may use it after perfoming click..Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-90930031221508490842010-01-01T18:24:01.676+01:002010-01-01T18:24:01.676+01:00Hi, Amit,
The standard Spinner implementation doe...Hi, Amit,<br /><br />The standard Spinner implementation does not allow you to do this because the Adapter backing the dropdown list is derived from the Adapter that is backing the Spinner view itself. This can be easily changed, however.<br /><br />Create a custom view based on the platform's Spinner class.<br /><br /><a href="http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob_plain;f=core/java/android/widget/Spinner.java;hb=HEAD" rel="nofollow"><br />Source of the Spinner class<br /></a><br /><br />Then modify the source in such a way that the adapter backing the Spinner and the adapter backing the dropdown list are separated.Gabor Pallerhttps://www.blogger.com/profile/14307475522972458932noreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-22603099965940387052009-12-31T22:57:46.350+01:002009-12-31T22:57:46.350+01:00Thanks for explaining it. I need to do something l...Thanks for explaining it. I need to do something little different. <br /><br />I want spinner lable to always say a constant string say "More...". When you click on it, it needs to show array (as usual) and on select of any item in it, display "More..." on the screen instead of displaying label of selected item. How do I do this?Amitnoreply@blogger.comtag:blogger.com,1999:blog-8214401912480503366.post-34167122818733445192009-10-29T10:18:55.098+01:002009-10-29T10:18:55.098+01:00Hi,
Great explanation!!!
I would also like to po...Hi,<br /><br />Great explanation!!!<br /><br />I would also like to point out that instead of maintaining the array of MyData, you could take it out from the initialization of the adapter and add items after the initialization. You could then use the adapter's getItem method to get the selected item.<br /><br />ArrayAdapter<MyData> adapter = <br />new ArrayAdapter<MyData>( <br />this,<br />android.R.layout.simple_spinner_item);<br /><br />adapter.add(new MyData("key1", "value1"));<br />adapter.add(new MyData("key2", "value2"));<br />adapter.add(new MyData("key3", "value3"));<br /><br />To retrieve the selected item in the onItemSelected method in the listener:<br /><br />MyData d = adapter.getItem(position);<br /><br />Thank you very much for your time. This has helped me a lot.<br /><br />SunDAnonymousnoreply@blogger.com