Stack Exchange
How to Retrive proper data on Spinner from SimpleCursorAdpter in Android
hay i used the SimpleCursorAdapter from implement the data on the spinner.its going well. also to get data i used following code: 
Cursor cursor = (Cursor) (spncomapnayname.getSelectedItem()); 
if (cursor != null) { 
    companyselected = cc.getString(cc.getColumnIndex(db.COMPANY_NAME)); 
} 

it is working well.but it is for the getting data from the spinner.but now i want to set particular data on spinner which was inserted by the user for update for normal ArrayAdapter spinner we used:
spinnername.setSelection(adapter1.getPosition(abc)); 

so i want to know how to set data from the database on spinner.
here is my code: 
cursor = db.getProductName(); 
adapter1 = new SimpleCursorAdapter(this, android.R.layout.simple_spinner_item, cursor, new String[] { DB_Database.PRODUCT_NAME }, new int[] { android.R.id.text1 });
adapter1.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
spnproduct.setAdapter(adapter1);


purchaseproduct = purchasebundle.getString("clickedpurchaseproductname");
if(!purchaseproduct.equals("null")|| !purchaseproduct.equals("")) {
    System.out.println("purchaseproduct" + purchaseproduct); 
    String mypurchaseproduct=purchaseproduct;
    ArrayAdapter myAdap = (ArrayAdapter) spnproduct.getAdapter(); 
    int spinnerPosition = myAdap.getPosition(mypurchaseproduct); // set the default according to value 
    spnproduct.setSelection(spinnerPosition);
}

here i get data from bundle.i want to retrive data on Spinner.the data is populated from SimpleCursorAdapter

First Get Selected Value From Cursor and store into mString Variable and then use below code for display this selected value into Spinner.
if (!mString.equals("null") || !mString.equals("")) {
    String myString = mString; // the value you want the position for
    ArrayAdapter<String> myAdap = (ArrayAdapter<String>) mSpn1.getAdapter(); // cast to an ArrayAdapter
    int spinnerPosition = myAdap.getPosition(myString);
    // set the default according to value
    mSpn1.setSelection(spinnerPosition);
}


hay i used that code but it was shown me a error java.lang.ClassCastException: android.widget.SimpleCursorAdapter. my spinner had populated data with SimpleCursorAdapter and used the ArrayAdapter may be because of that is throws a error.

purchaseproduct=purchasebundle.getString("clickedpurchaseproductname");    if(!purchaseproduct.equals("null")|| !purchaseproduct.equals(""))
{
System.out.println("purchaseproduct" + purchaseproduct);
String mypurchaseproduct=purchaseproduct;
ArrayAdapter myAdap = (ArrayAdapter) spnproduct.getAdapter();
int spinnerPosition = myAdap.getPosition(mypurchaseproduct);
// set the default according to value
spnproduct.setSelection(spinnerPosition);
}

@Kirti Post your full code into your question so it's easy to understand.

@Kirti sorry for that but i don't know about this.

You can do something like this:
Cursor c = (Cursor)parent.getItemAtPosition(pos);
int id = c.getInt(c.getColumnIndexOrThrow("columm_name"));

or to get a String
String name=c.getString(c.getColumnIndexOrThrow("columm_name"));

