Android Spinner Selected Item
Asked Answered
M

6

6

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

Men answered 28/4, 2011 at 12:48 Comment(0)
L
9

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....

Lenity answered 29/4, 2011 at 16:49 Comment(0)
W
5

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));
Whaleboat answered 28/4, 2011 at 12:51 Comment(4)
is there a way like in HTML to have the name and key value like in the option select options.Men
What exactly do you mean by key valueWhaleboat
<select> <option value="1">Volvo</option> <option value="2">Saab</option> </select> like this. The display text is Car names but values are idsMen
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!Lump
O
1
    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
Oedipus answered 8/7, 2011 at 11:11 Comment(0)
E
0

The way I achieved it was like this:

You will have to cast the getItem() method to a cursor as it is essentially just returning a cursor rather than an individual row in the cursor which is annoying.

final Cursor cursor = (Cursor) yourSpinner.getAdapter().getItem( position );

Move the cursor to the first row

cursor.moveToFirst();

Now you can get the string from the cursor using the column name that matches table originally queried when setting your spinners adapter which was grabbed above using getAdapter()

final String selectedPartUUID = cursor.getString( cursor.getColumnIndex( TABLE_COLUMN_NAME ) );

Hope this helps and would appreciate any feedback :)

Effluence answered 2/12, 2013 at 16:40 Comment(0)
G
0

Hope this helps!!

Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();
Graecize answered 10/1, 2015 at 14:20 Comment(0)
L
0
 public List<String> GetTexts() {
            ArrayList<String> texts = new ArrayList<String>();
            for (int i = 0; i < mSpinnerList.size(); i ++) {
                int position = mSpinnerList.get(i).getSelectedItemPosition();
                Log.d("DATA_TO_BE_SEND:", tennisModelArrayList.get(position).getStatus());
                texts.add(mSpinnerList.get(i).getItemAtPosition(i).toString());
            }

            return texts;
        }
Lumisterol answered 24/12, 2022 at 14:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.