What I’m trying to do is, I’m asking user to select a value from a list view, after which he is redirected to another screen having a spinner with default as selected by the user . I’ve read quite a few posts on spinner’s default but none of then included fetching info from another screen, I’ve tried them anyway but they didn’t work.
This is how I pass the string from WorkEntryScreenActivity.java
//inside onCreate
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> myAdapter, View myView,
int myItemInt, long mylng) {
selectedFromList = (String) (mListView.getItemAtPosition(myItemInt));
top10 = true;
call(top10); }
});
//outside onCreate
protected void call(boolean top10){
if(top10) {
Intent i = new Intent(this, WorkEntryActivity.class);
i.putExtra("FromTab", "true");
i.putExtra("workRequest",selectedFromList);
startActivity(i);
finish();
}
}
In the other java file WorkEntryActivity.java
//get the string
workRequestFetched = extras.getString("workRequest");
//This is where I need help
ArrayList<String> workRequestAdded = new ArrayList<String>();
workRequestSpinner = (Spinner) findViewById(R.id.workRequestSpinner);
if(workRequestFetched != null){
workRequestAdded.add(workRequestFetched);
}
ArrayList<String> workRequests = ServiceCall.workRequests;
workRequestAdded.addAll(workRequests);
final ArrayAdapter<String> workRequestAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,
workRequestAdded);
workRequestAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
workRequestSpinner.setAdapter(workRequestAdapter);
If I remember correctly, the spinner’s default option is the first option in the String array associated with it. If you make workRequest the first option in the string array, it should be the default option.
EDIT: After some googling it seems that you cannot modify your res/Strings.xml programmatically. However I would be surprised if there isn’t a way to link a String array defined in your relevant java file to your spinner.