How can I see a value on the spinner?
My question is: I am using spinner on my android app. However, I can't see the default value shown on the spinner, at all. I can select elements but I can't see any text on the spinner. It seems like the value is hidden and doesn't show anything, just the spinner itself and drop down arrow.
mDbHelper = new DbAdapter(this);
mDbHelper.open();
cursor = mDbHelper.fetchAllBusinessCards();
startManagingCursor(cursor);
contactSpinner = (Spinner) findViewById(R.id.contactSpinner);
contactSpinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
fillData();
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
private void fillData() {
/*Create an array to specify the fields we want to display in the list (only the 'colourName' column in this case) */
String[] from = new String[]{DbAdapter.getKeyTitle() };
/* and an array of the fields we want to bind those fields to (in this case just the textView 'tvDBViewRow' from our new db_view_row.xml layout above) */
int[] to = new int[]{R.id.tvDBViewRow};
/* Now create a simple cursor adapter.. */
SimpleCursorAdapter colourAdapter =
new SimpleCursorAdapter(this,R.layout.db_view_row, cursor, from, to);
/* and assign it to our Spinner widget */
contactSpinner.setAdapter(colourAdapter);
//contactSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
contactSpinner.setSelection(0);
}
@开发者_运维知识库Override
protected void onDestroy() {
super.onDestroy();
if (mDbHelper != null) {
mDbHelper.close();
}
}
}
You can call spinner.setSelection to set the current state of the spinner to whatever you want. And that definitely works
spinner.setSelection(0);
but you must also call setDropDownViewResource()
let say
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
精彩评论