I filled a spinner with a query.
How can I retrieve the information that I selected? I’m having trouble with getSelectedItem(position)..
I have a cursor where I save the query, I fill the spinner that’s no problem. But I don’t know how to assign to a string the variable I selected in the spinner.
For example
spinnerNAME.setOnItemSelectedListener(
new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent,
android.view.View v, int position, long id) {
selEntidad = ??;
}
public void onNothingSelected(AdapterView<?> parent) {
selEntidad ="noData";
}
});
How can I put in selEntidad the value of what I selected in the spinner, that I filled with a query?
If you pass strings to the spinner you should be able to get the corresponding string with
(String) parent.getItemAtPosition(position).getItemAtPositionwill always return the object the adapter uses to construct the views. So in case of aSimpleCursorAdapteryou get the cursor at this position. You can cast the returned object toCursorand then query your data. E.g.getString(0)if you have a string in the first column of the queried cursor.