Harsha M V
Harsha M V

Reputation: 54949

Android Spinner Selected Item

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

Upvotes: 6

Views: 18254

Answers (6)

ranojan
ranojan

Reputation: 837

 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;
        }

Upvotes: 0

Shukana789
Shukana789

Reputation: 1

Hope this helps!!

Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
            Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();

Upvotes: 0

Sambuxc
Sambuxc

Reputation: 479

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 :)

Upvotes: 0

Urvesh Patel
Urvesh Patel

Reputation: 11

    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

Upvotes: 1

Farhan
Farhan

Reputation: 13390

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

Upvotes: 9

Mojo Risin
Mojo Risin

Reputation: 8142

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));

Upvotes: 5

Related Questions