问题
I wanted to display selected item in the textView
when selected from dropdown list of spinner I implemented AdapterView.OnItemSelectedListener
but when I'm selecting item its always null/empty
here is my code:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new CustomAdapter(MainActivity.this, Languages));
btn.setOnClickListener(this);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
item = (String)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
回答1:
SO answer source: https://stackoverflow.com/a/49301966/5461982
I know this is an older question now but if anyone else comes across it be sure to check that your custom implementation of the Adapter you're setting for your Spinner overrides the getItem
method:
@Override
public Object getItem(int position) {
return spinnerItems.get(position);
}
By default, you're required to override this method when implementing a custom BaseAdapter
but the default return type is null
. Be sure to modify the return type to return spinnerItems.get(position)
.
Hope this helps, I spent around 30 mins trying to work this out originally!
回答2:
Try this,
String selected_item = spinner.getSelectedItem().toString();
回答3:
Just retrieve value from String Array:
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final String item = Languages[position];
Toast.makeText(MainActivity.this, item, Toast.LENGTH_SHORT).show();
}
回答4:
try this
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
sSelectedItem = arr_spiner.get(spinner.getSelectedItemPosition())
.getName();
txtSpinnerValue.setText(sSelectedItem);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
https://stackoverflow.com/questions/45159011/spinner-item-is-not-visible-data-is-coming-from-server-android/45161202#45161202
回答5:
Try This
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setAdapter(new CustomAdapter(MainActivity.this, Languages));
btn.setOnClickListener(this);
spinner.setOnItemSelectedListener(this);
}
@Override
public void onItemSelected(AdapterView<?> parent,
View view, int position, long id) {
//item = (String)parent.getItemAtPosition(position);
item = (String) spinner.getSelectedItem().toString(); //Here is the Change
Toast.makeText(MainActivity.this, item.toString(),
Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
The change here is
From
item = (String)parent.getItemAtPosition(position);
TO
item = (String) spinner.getSelectedItem().toString();
来源:https://stackoverflow.com/questions/45188309/spinner-getitematpositionposition-is-returning-always-null-empty