'Preset spinner in onCreate is overwritten
In an activity I have a spinner where the user can select which date should be displayed in the activity.
But in the previous activity there can be a preselection done. This preselection is passed to the activity by intent.putExtra
.
The problem is that when I want to set the preselection with setSelection
in the onCreate
function it is always overwritten by the onItemSelected
function.
How can I do this in the right way?
The code locks like this:
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set layout
setContentView(R.layout.activity_list);
setTitle(getString(R.string.List_Title));
// for alerts
builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
// RecyclerView
adapter = new ListAdapter_rc(this);
lvArticle = (RecyclerView)findViewById(R.id.listView);
lvArticle.setAdapter(adapter);
lvArticle.setLayoutManager(new LinearLayoutManager(this));
new ItemTouchHelper(itemTouchCallback).attachToRecyclerView(lvArticle);
spListSelect = (Spinner)findViewById(R.id.spListSelect);
adapter.dbm = new DBManager(this);
ListAdapter_rc a = adapter;
ArrayList<String> slDummy = new ArrayList<>();
slDummy.addAll(adapter.dbm.listListGetNames());
slDummy.add("Default");
ArrayAdapter<String> adapt = new ArrayAdapter<String>(this, R.layout.spinner_item, slDummy);
spListSelect.setAdapter(adapt);
spListSelect.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
adapter.setList(adapter.dbm.listListGet().get(i).id);
// THIS IS CALLED SECOND (i is always 0)
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {}
});
Bundle extras = getIntent().getExtras();
if (extras != null) {
adapter.adminMode = extras.getBoolean("adminMode");
int i = (int)extras.getLong("listSelcted", 0);
adapter.setList((long)i);
spListSelect.setSelection(i,false); // THIS IS CALLED FIRST (i has the desired value)
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|