0

I just need to add setselection method to my spinner by using the position that get from database.

I already try both(in is the position of text)

spinner.setselection(in);

spinner.setselection(in,true);

but it always shows first object in the array that used by the spinner

thanks for help

Saurabh Vardani
  • 1,821
  • 2
  • 18
  • 33
Shellz
  • 99
  • 1
  • 14

3 Answers3

4

try this it will work

  for (int i = 0; i < array.size(); i++) {
                if (postion == Integer.parseInt(array.get(i))) {
                    spinner.setSelection(i);
                    break;
                }
            }
megha jagdale
  • 396
  • 2
  • 4
2

Have you tried using the name of the item to get the position?

spinner.setSelection(((ArrayAdapter)spinner.getAdapter()).getPosition("Item Name​"));
enifeder
  • 487
  • 1
  • 6
  • 16
  • I fetch the item from database – Shellz Apr 19 '16 at 05:41
  • mcursor = sq.rawQuery("SELECT * FROM " + DatabaseHelper.TABLE_NAME + " where "+ DatabaseHelper.KEY_NAME + " =?",new String[]{grp}); if (mcursor.moveToFirst()) { do { in=(mcursor.getString(mcursor.getColumnIndex(DatabaseHelper.KEY_ID))); } while (mcursor.moveToNext()); – Shellz Apr 19 '16 at 05:41
1

Make sure your setSelection() is called after you done with spinner's setAdapter().

Godfather
  • 833
  • 9
  • 14