Android Spinner Selected Item

前端 未结 6 2163
花落未央
花落未央 2020-12-14 13:13

I am populating a spinner from the database like this

    // Populating the City Spinner
    Cursor cities = db.cityList();
    startManagingCursor(cities);
         


        
相关标签:
6条回答
  • 2020-12-14 13:42

    Hope this helps!!

    Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString, Toast.LENGTH_SHORT).show();
                Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos, Toast.LENGTH_SHORT).show();
    
    0 讨论(0)
  • 2020-12-14 13:49

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

    0 讨论(0)
  • 2020-12-14 14:03
        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
    
    0 讨论(0)
  • 2020-12-14 14:03

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

    0 讨论(0)
  • 2020-12-14 14:04

    Get spinner selected items text?

    0 讨论(0)
  • 2020-12-14 14:06

    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));
    
    0 讨论(0)
提交回复
热议问题