How To Update An Spinner Dynamically Correctly?
I have a spinner with a custom adapter displaying objects from a database. When the object list changed I create a new adapter with the List and apply it on the spinner. Afterwards
Solution 1:
Solved: I guess the main problem was the custom spinner adapter. This works fine now
if (spinner.getCount() > 0) {
pos = spinner.getSelectedItemPosition();
}
MySpinnerAdapteradapter=newMySpinnerAdapter(context, myNewObjects);
spinner.setAdapter(adapter);
spinner.setSelection(pos); // needed
adapter.notifyDataSetChanged();
Solution 2:
spinner.setOnItemSelectedListener(newOnItemSelectedListener() {
@OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
spinner.setSelectedItem(5);
}
@OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
Solution 3:
Did u tried notifydatasetChanged()
or notifydatasetInvalidate()
method of adapter.
newAdapter.notifydatasetChanged()
Post a Comment for "How To Update An Spinner Dynamically Correctly?"