Android Spinner Selected Item

17,538

Solution 1

I think, as you are using a customadapter and giving three lists in adapter...

you can't get the selected text simply by calling the getSelectedItem()..

Use this:

Spinner mySpinner = (Spinner)findViewbyId(R.id.spinner);
int position = mySpinner.getSelectedItemPosition();
String Text = yourCityList[position].toString(); // dont know which list is holding the city list... 
// i didn't use any db in any of my app so cant tell you how can you get list... 
// leaving it to you... :)

Hope it helps....

Solution 2

Just get the adapter from your spinner and get the string from the cursor

Cursor cursor = (Cursor) myAdapter.getItem(position);
String cityName = cursor.getString(cursor.getColumnIndex(DBAdapter.KEY_NAME));

Solution 3

    List<String> list = new ArrayList<String>();
    s = (Spinner)findViewById(R.id.spinner1);
    SQLiteDatabase sqldb = db.openDataBase();
    Cursor cr = sqldb.rawQuery("select Name from employee",null);
    if (cr.moveToFirst()) {
        do {
        list.add(cr.getString(0).toString());
        Toast.makeText(this,cr.getString(0).toString(),Toast.LENGTH_LONG).show();
        ArrayAdapter <String> a= new ArrayAdapter(this, android.R.layout.simple_spinner_item,list);
         a.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
          s.setAdapter(a);
        } while (cr.moveToNext());

      }//urvesh patel
Share:
17,538
Harsha M V
Author by

Harsha M V

I turn ideas into companies. Specifically, I like to solve big problems that can positively impact millions of people through software. I am currently focusing all of my time on my company, Skreem, where we are disrupting the ways marketers can leverage micro-influencers to tell the Brand’s stories to their audience. People do not buy goods and services. They buy relations, stories, and magic. Introducing technology with the power of human voice to maximize your brand communication. Follow me on Twitter: @harshamv You can contact me at -- harsha [at] skreem [dot] io

Updated on August 16, 2022

Comments

  • Harsha M V
    Harsha M V almost 2 years

    I am populating a spinner from the database like this

        // Populating the City Spinner
        Cursor cities = db.cityList();
        startManagingCursor(cities);
    
        // create an array to specify which fields we want to display
        String[] from = new String[] { DBAdapter.KEY_NAME };
        // create an array of the display item we want to bind our data to
        int[] to = new int[] { android.R.id.text1 };
    
        Spinner cityList = (Spinner) this.findViewById(R.id.citySpiner);
        SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cities, from, to);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        cityList.setAdapter(adapter);
    

    When i try to get the content from the selected item of the spinner like this

    // Get the City
                    Spinner getCity = (Spinner) findViewById(R.id.citySpiner);
                    String cityName = getCity.getSelectedItem().toString();
    

    i get the following. Is there a way i can get the city name or the city id from the database.

    enter image description here

  • Harsha M V
    Harsha M V about 13 years
    is there a way like in HTML to have the name and key value like in the option select options.
  • Mojo Risin
    Mojo Risin about 13 years
    What exactly do you mean by key value
  • Harsha M V
    Harsha M V about 13 years
    <select> <option value="1">Volvo</option> <option value="2">Saab</option> </select> like this. The display text is Car names but values are ids
  • Luis
    Luis over 11 years
    This works great, thanks! I was just looking for a way to get the cursor-loaded spinner's selected value as String without having to do a listener on the spinner. Works like a charm!