Very Weird Spinner Issue

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Very Weird Spinner Issue

Postby insanomania911 » Wed Jul 01, 2009 9:24 pm

Hello,
I am trying (as many are doing) to populate a 2nd spinner out of the
first spinner selected item like this:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. //populate first spinner
  2.  
  3.             final Spinner s = (Spinner) findViewById(R.id.spinner);
  4.  
  5.             ArrayAdapter adapter = ArrayAdapter.createFromResource(
  6.  
  7.                     this, R.array.universities,
  8.  
  9. android.R.layout.simple_spinner_item);
  10.  
  11.             adapter.setDropDownViewResource
  12.  
  13. (android.R.layout.simple_spinner_dropdown_item);
  14.  
  15.             s.setAdapter(adapter);
  16.  
  17. // Initialization of the 2nd spinner
  18.  
  19.                    String[] array = {"Select Building Here"};
  20.  
  21.                    final Spinner s2 = (Spinner) findViewById(R.id.spinner2);
  22.  
  23.                    ArrayAdapter<String> cadapter = new ArrayAdapter(this,
  24.  
  25. android.R.layout.simple_spinner_item, array);
  26.  
  27.                    cadapter.setDropDownViewResource
  28.  
  29. (android.R.layout.simple_spinner_dropdown_item);
  30.  
  31.                    s2.setAdapter(cadapter);
  32.  
  33.                 // When user selects a university
  34.  
  35.                     s2.setOnItemSelectedListener(new OnItemSelectedListener() {
  36.  
  37.                                 @Override
  38.  
  39.                                 public void onItemSelected(AdapterView<?> arg0, View arg1,
  40.  
  41.                                                 int arg2, long arg3) {
  42.  
  43.                                                  //Get the selected building
  44.  
  45.                                            String selectedItemString = (String) s2.getSelectedItem();
  46.  
  47.                                            System.out.println
  48.  
  49. ("Building: !!!"+selectedItemString);
  50.  
  51.                                 }
  52.  
  53.                                 @Override
  54.  
  55.                                 public void onNothingSelected(AdapterView<?> arg0) {
  56.  
  57.                                         // TODO Auto-generated method stub
  58.  
  59.                                 }
  60.  
  61.                });
  62.  
  63.             // When user selects a university
  64.  
  65.             s.setOnItemSelectedListener(new OnItemSelectedListener() {
  66.  
  67.                         @Override
  68.  
  69.                         public void onItemSelected(AdapterView<?> arg0, View arg1,
  70.  
  71.                                         int arg2, long arg3) {
  72.  
  73.                                   //Get the selected university
  74.  
  75.                                    String selectedItemString = (String) s.getSelectedItem();
  76.  
  77.                                    System.out.println
  78.  
  79. ("Uni: !!!"+selectedItemString);
  80.  
  81.                           if(selectedItemString .equals(""))
  82.  
  83.                           {
  84.  
  85.                                   // In case of an error so it won't crash
  86.  
  87.                                   nospinner();
  88.  
  89.                           }
  90.  
  91.                           else
  92.  
  93.                           {
  94.  
  95.                                   // Find the table of the selected University dynamically
  96.  
  97.                                   finder(selectedItemString);
  98.  
  99.                           }
  100.  
  101.                         }
  102.  
  103.                         @Override
  104.  
  105.                         public void onNothingSelected(AdapterView<?> arg0) {
  106.  
  107.                                 // TODO Auto-generated method stub
  108.  
  109.                         }
  110.  
  111.        });
  112.  
  113. where finder() is
  114.  
  115. protected void finder(String selectedItemString) {
  116.  
  117.                final Dbadapter db = new Dbadapter(this);  // This creates an
  118.  
  119. instance of the database in this class
  120.  
  121.            final Spinner s2 = (Spinner) findViewById(R.id.spinner2);
  122.  
  123.                    // Access Database
  124.  
  125.                    db.open();
  126.  
  127.                    // Fix selected item to match the db
  128.  
  129.                    selectedItemString = (selectedItemString).toLowerCase();
  130.  
  131.                    selectedItemString = selectedItemString.replaceAll(" ","");
  132.  
  133.                 //   System.out.println("Uni: !!!!!!!!!!!! >>>>"+selectedItemString+"<<<<<<");
  134.  
  135.  
  136.  
  137.                    // Call a function to return the rows of that university
  138.  
  139.                    final Cursor cur = db.getAllTitles(selectedItemString);
  140.  
  141.            // Get Columns
  142.  
  143.            String[] columns = new String[]{"building"};
  144.  
  145.                    SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this,
  146.  
  147. android.R.layout.simple_spinner_item, cur,
  148.  
  149.                        columns, new int[] {android.R.id.text1});
  150.  
  151.                    // Spinner Layout and population
  152.  
  153.                    cadapter.setDropDownViewResource
  154.  
  155. (android.R.layout.simple_spinner_dropdown_item);
  156.  
  157.                    s2.setAdapter(cadapter);
  158.  
  159.                    db.close();
  160.  
  161. }
Parsed in 0.048 seconds, using GeSHi 1.0.8.4


whenever the user changes the selection of the first spinner it's
fine, whenever the changes are related to the 2nd spinner I get a
handler exception on this line:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. String selectedItemString = (String)
  2.  
  3. s2.getSelectedItem();
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

I could get an object on selected item but not the actual String
value.
anyone could help?
thanks a lot
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Top

Postby nmc » Wed Jul 01, 2009 11:21 pm

I get a handler exception on this line: String selectedItemString = (String)
s2.getSelectedItem();
I could get an object on selected item but not the actual String value.


My first guess: String selectedItemString = s2.getSelectedItem().toString();
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby insanomania911 » Thu Jul 02, 2009 3:45 pm

but that doesn't give me the selected item, in fact it gives something like this:

android.database.sqlite.SQLCursor@43748790
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby nmc » Thu Jul 02, 2009 6:56 pm

I have never used CursorAdapter myself, but i would assume that you could get the string from the SQLCursor.
If the list can do, you should be able to do it also 8)
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby insanomania911 » Thu Jul 02, 2009 7:52 pm

but I am already getting the values populated in the spinner. I just can't put the selected value inside a variable.

I can't figure out a way to extract the string out of: String selected = s2.getSelectedItem().toString();
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby nmc » Thu Jul 02, 2009 8:18 pm

You said the object is of type android.database.sqlite.SQLiteCursor, so just visit the android reference to find out:

http://developer.android.com/reference

(see methods inherited from android.database.Cursor)
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Top

Postby insanomania911 » Mon Jul 06, 2009 4:10 pm

yes, i did and there's nothing concerning getting the actual value. There's getCount() , Index.. but not the actual string value

thanks
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby nmc » Mon Jul 06, 2009 6:53 pm

I saw that "methods inherited from android.database.Cursor" including .getString(rowID), with this you should be able to get the needed data.
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby insanomania911 » Mon Jul 06, 2009 6:57 pm

I am sorry but can you write out the whole line?


why does it work in the first spinner but not in the second one?

this worked in the first one: String selected = (String) s1.getSelectedItem();

do you think its a VIEW issue? both of them has this view

public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

thank you,
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby nmc » Mon Jul 06, 2009 7:28 pm

I am sorry but can you write out the whole line?


Would be something like
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SQLCursor c = (SQLCursor)s2.getSelectedItem();
  2. String str = c.getString(0);
  3.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


why does it work in the first spinner but not in the second one?


Because the first adapter uses a string-array while the second one uses a database-cursor?
nmc
Senior Developer
Senior Developer
 
Posts: 154
Joined: Thu Nov 27, 2008 8:30 pm
Location: Germany

Postby insanomania911 » Mon Jul 06, 2009 7:46 pm

that returns the index only. for example it returned 3

thanks a lot for your fast replies
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Postby insanomania911 » Mon Jul 06, 2009 7:50 pm

ah got it got it!!!

String selected = c.getString(2); worked for me // just realized the number inside was not the row but the column id



you're the best!!!!!!!!! that took me 3 weeks without solving!!!!
insanomania911
Junior Developer
Junior Developer
 
Posts: 12
Joined: Tue Jun 09, 2009 4:53 pm

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 9 guests