0

I have created a spinner and the items of spinner comes from database. However, When I use

public class MyOnItemSelectedListener implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
        View view, int pos, long id) {
        typeOFBCard = contactSpinner.getSelectedItem().toString();
    }

    public void onNothingSelected(AdapterView parent) {
      // Do nothing.
    }
}

When I call this listener and try to pick the chosen string of the spinner i get a reference of the sglite something like:

android.database.sqlite.SQLiteCursor@40535568

This is the return value of typeOfBCard.

However, on the spinner I can see normal string like "Work".

Here is how I initialized the spinner :

contactSpinner = (Spinner) findViewById(R.id.contactSpinner);
    mobileText =(EditText) findViewById(R.id.mobileText);
    mDbHelper = new DbAdapter(this);
    mDbHelper.open();
    cursor = mDbHelper.fetchAllBusinessCards();
    startManagingCursor(cursor);
    context =this;

    contactSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
ark
  • 167
  • 1
  • 1
  • 14
akd
  • 6,538
  • 16
  • 70
  • 112

2 Answers2

0

Every row in a spinner is a view but it's also a value/object from your source. Try

public class MyOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent,
    View view, int pos, long id) {
     // Parent == where the click happened. 
    typeOFBCard = parent.getSelectedItem().toString();
}

public void onNothingSelected(AdapterView parent) {
  // Do nothing.
}
}
EdChum
  • 376,765
  • 198
  • 813
  • 562
user1278085
  • 11
  • 1
  • 3
0

How ever on the spinner I can see normal string like "Work"

That is because you configured an Adapter on the Spinner, and the Adapter is pulling data out of the Cursor to display.

How to get spinner string value on android?

There is no "spinner string value". Spinners don't have strings. They have views. Those views might be instances of TextView, or they might be instances of ImageView, or they might be instances of a LinearLayout holding onto a TextView and an ImageView, or...

If you want to get data out of the Cursor, call getString() on the Cursor.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491