When the user enters the Activity, there will be 4 spinners that he needs to fill out and then click on a submit button.
A progress dialog appears and the user will then get redirected back to the prior activity.
In case the user doesn’t fill out all necessary data the app gets an NPE.
Is it possible to state that if the user doesn’t select anything in the spinner, the spinner will choose the first variable in the string and that will be the variable to get passed to the next activity?
All of my spinners have “NIL” as the first value which is an abbreviation for “Nothing in line”
So if the spinner is empty = NIL
Thank you for your time!
CODE
submit.setOnClickListener(v -> { progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(false); progressBar.setTitle("Sending Data ..."); progressBar.setMessage("Redirecting to previous page"); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); progressBarStatus = 0; fileSize = 0; progressBar.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { openwhatsActivity(); } }); new Thread(() -> { while (progressBarStatus <100) { progressBarStatus = doOperation(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBarHandler.post(() -> progressBar.setProgress(progressBarStatus)); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.dismiss(); }).start(); }); } public int doOperation() { while (fileSize <= 10000) { fileSize++; if (fileSize == 1000) { return 10; }else if (fileSize == 2000) { return 20; } } return 100; } public void openwhatsActivity() { String roadtext = roadwheelsspinner.getSelectedItem().toString(); String separationtext = separationspinner.getSelectedItem().toString(); String sprockettext = sprocketwheelspinner.getSelectedItem().toString(); String returntext = returnrollersspinner.getSelectedItem().toString(); Intent intent = new Intent(this, whatsActivity.class); intent.putExtra(EXTRA_ROADTEXT, roadtext); intent.putExtra(EXTRA_SEPARATIONTEXT, separationtext); intent.putExtra(EXTRA_SPROCKETTEXT, sprockettext); intent.putExtra(EXTRA_RETURNTEXT, returntext); startActivity(intent); }
}
EDIT
I may have missed some details that could make my question more clear…
This is how the spinner looks:
There’s a kind of hint in the spinner before the user interacts with it.
When the user interacts with it, the first possible choice will be “NIL”.
The user will choose “NIL” if he has nothing to declare about the subject.
But in case the user doesn’t interacts with it the default value should be “NIL”.
Code with spinners included:
Spinner roadwheelsspinner = findViewById(R.id.roadwheelsspinner); ArrayAdapter<CharSequence> roadadapter = ArrayAdapter.createFromResource(this, R.array.roadnumbers, android.R.layout.simple_spinner_item); roadadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); roadwheelsspinner.setAdapter(new NothingSelectedSpinnerAdapter(roadadapter, R.layout.contact_spinner_row_nothing_selected_road, this)); Spinner separationspinner = findViewById(R.id.separationspinner); ArrayAdapter<CharSequence> separationadapter = ArrayAdapter.createFromResource(this, R.array.separationnumbers, android.R.layout.simple_spinner_item); separationadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); separationspinner.setAdapter(new NothingSelectedSpinnerAdapter(separationadapter, R.layout.contact_spinner_row_nothing_selected_separation, this)); Spinner sprocketwheelspinner = findViewById(R.id.sprocketwheelspinner); ArrayAdapter<CharSequence> sprocketadapter = ArrayAdapter.createFromResource(this, R.array.sprocketwheelnumbers, android.R.layout.simple_spinner_item); sprocketadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); sprocketwheelspinner.setAdapter(new NothingSelectedSpinnerAdapter(sprocketadapter, R.layout.contact_spinner_row_nothing_selected_sprocket, this)); Spinner returnrollersspinner = findViewById(R.id.returnrollersspinner); ArrayAdapter<CharSequence> returnadapter = ArrayAdapter.createFromResource(this, R.array.returnrollersnumbers, android.R.layout.simple_spinner_item); returnadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); returnrollersspinner.setAdapter(new NothingSelectedSpinnerAdapter(returnadapter, R.layout.contact_spinner_row_nothing_selected_return, this)); Spinner axlesspinner = findViewById(R.id.axlesspinner); ArrayAdapter<CharSequence> axlesadapter = ArrayAdapter.createFromResource(this, R.array.axlesnumbers, android.R.layout.simple_spinner_item); axlesadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); axlesspinner.setAdapter(new NothingSelectedSpinnerAdapter(axlesadapter, R.layout.contact_spinner_row_nothing_selected_axles, this)); submit.setOnClickListener(v -> { progressBar = new ProgressDialog(v.getContext()); progressBar.setCancelable(false); progressBar.setTitle("Sending Data ..."); progressBar.setMessage("Redirecting to previous page"); progressBar.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); progressBar.setProgress(0); progressBar.setMax(100); progressBar.show(); progressBarStatus = 0; fileSize = 0; progressBar.setOnDismissListener(new DialogInterface.OnDismissListener() { @Override public void onDismiss(DialogInterface dialog) { openwhatsActivity(); } }); new Thread(() -> { while (progressBarStatus <100) { progressBarStatus = doOperation(); try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBarHandler.post(() -> progressBar.setProgress(progressBarStatus)); } try { Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } progressBar.dismiss(); }).start(); }); } public int doOperation() { while (fileSize <= 10000) { fileSize++; if (fileSize == 1000) { return 10; }else if (fileSize == 2000) { return 20; } } return 100; } public void openwhatsActivity() { String roadtext = roadwheelsspinner.getSelectedItem().toString(); String separationtext = separationspinner.getSelectedItem().toString(); String sprockettext = sprocketwheelspinner.getSelectedItem().toString(); String returntext = returnrollersspinner.getSelectedItem().toString(); Intent intent = new Intent(this, whatsActivity.class); intent.putExtra(EXTRA_ROADTEXT, roadtext); intent.putExtra(EXTRA_SEPARATIONTEXT, separationtext); intent.putExtra(EXTRA_SPROCKETTEXT, sprockettext); intent.putExtra(EXTRA_RETURNTEXT, returntext); startActivity(intent); }
}
Sorry for my bad spelling/grammar.. english isn’t my first language.
EDIT EDIT
I solved my issue.
I’m using an adapter that got posted some years back on this page by a user giving a great solution for how to get a view in the nothing selected display.
Link here: https://stackoverflow.com/a/12221309/16516988
SpinnerAdapter code:
public class NothingSelectedSpinnerAdapter implements SpinnerAdapter, ListAdapter { protected static final int EXTRA = 1; protected SpinnerAdapter adapter; protected Context context; protected int nothingSelectedLayout; protected int nothingSelectedDropdownLayout; protected LayoutInflater layoutInflater; public NothingSelectedSpinnerAdapter( SpinnerAdapter spinnerAdapter, int nothingSelectedLayout, Context context) { this(spinnerAdapter, nothingSelectedLayout, -1, context); } public NothingSelectedSpinnerAdapter(SpinnerAdapter spinnerAdapter, int nothingSelectedLayout, int nothingSelectedDropdownLayout, Context context) { this.adapter = spinnerAdapter; this.context = context; this.nothingSelectedLayout = nothingSelectedLayout; this.nothingSelectedDropdownLayout = nothingSelectedDropdownLayout; layoutInflater = LayoutInflater.from(context); } @Override public final View getView(int position, View convertView, ViewGroup parent) { if (position == 0) { return getNothingSelectedView(parent); } return adapter.getView(position - EXTRA, null, parent); } protected View getNothingSelectedView(ViewGroup parent) { return layoutInflater.inflate(nothingSelectedLayout, parent, false); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { if (position == 0) { return nothingSelectedDropdownLayout == -1 ? new View(context) : getNothingSelectedDropdownView(parent); } return adapter.getDropDownView(position - EXTRA, null, parent); } protected View getNothingSelectedDropdownView(ViewGroup parent) { return layoutInflater.inflate(nothingSelectedDropdownLayout, parent, false); } @Override public int getCount() { int count = adapter.getCount(); return count == 0 ? 0 : count + EXTRA; } @Override public Object getItem(int position) { return position == 0 ? 0 : adapter.getItem(position - EXTRA); } @Override public int getItemViewType(int position) { return 0; } @Override public int getViewTypeCount() { return 1; } @Override public long getItemId(int position) { return position >= EXTRA ? adapter.getItemId(position - EXTRA) : position - EXTRA; } @Override public boolean hasStableIds() { return adapter.hasStableIds(); } @Override public boolean isEmpty() { return adapter.isEmpty(); } @Override public void registerDataSetObserver(DataSetObserver observer) { adapter.registerDataSetObserver(observer); } @Override public void unregisterDataSetObserver(DataSetObserver observer) { adapter.unregisterDataSetObserver(observer); } @Override public boolean areAllItemsEnabled() { return false; } @Override public boolean isEnabled(int position) { return position != 0; }
}
What I did was: I changed the return position to “NIL” instead of 0. Now when nothing is declared in the spinner it will pass the value “NIL” to the next Activity.
Advertisement
Answer
All of my spinners have “NIL” as the first value
Just use spinner.setSelection(1)
for every spinner after populate them. Use that value as “default” so you don’t have to check if the user selected something, the spinners will always have something selected.