I am making an android app that asks for the users to select a country via spinner.
When the user opens the app first time, user selects a country from list.
<You can use SharedPreferences
to store the selection the first time that the user selects a country, and then use SharedPreferences
again for the app to remember the selection, when the user returns a second time.
To store the selection in a SharedPrefence:
SharedPreferences.Editor editor = getPreferences(0).edit();
int selectedPosition = yourSpinner.getSelectedItemPosition();
editor.putInt("spinnerSelection", selectedPosition);
editor.apply();
To load the selection onto the spinner when reusing the app:
SharedPreferences prefs = getPreferences(0);
yourSpinner.setSelection(prefs.getInt("spinnerSelection",0));
Hope this solves your issue :)
You can use sharedPreference to store the country you have chosen, then search the map and find the position of country in your array, finally use setSelection(int position)to set the default country
Try this one
String citySelected
final CharSequence[] items = {" abc ", " def ", " ghi ", " jkl ", " mno ",
" pqr ", " stu ",
" vwzyz "};
List<String> lanSelected = new ArrayList<>();
final boolean[] checkedItems = new boolean[]{false, false, false, false, false, false, false, false};
List<String> temp = new ArrayList<>();
for (int o = 0; o < items.length; o++) {
temp.add(items[o].toString());
}
final List<Integer> seletedItems = new ArrayList();
if (citySelected.equals("") || citySelected == null) {
} else
lanSelected = Arrays.asList(citySelected.split(","));
if (lanSelected.size() > 0) {
for (int p = 0; p < lanSelected.size(); p++) {
String x = lanSelected.get(p);
int xpos = temp.indexOf(x);
if (xpos != -1) {
checkedItems[xpos] = true;
seletedItems.add(xpos);
}
}
}
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(context, android.R.style.Theme_Material_Light_Dialog_Alert);
} else {
builder = new AlertDialog.Builder(context);
}
// arraylist to keep the selected items
AlertDialog dialog = builder
.setTitle("city")
.setMultiChoiceItems(items, checkedItems, new DialogInterface.OnMultiChoiceClickListener() {
@Override
public void onClick(DialogInterface dialog, int indexSelected, boolean isChecked) {
if (isChecked) {
// If the user checked the item, add it to the selected items
seletedItems.add(indexSelected);
} else if (seletedItems.contains(indexSelected)) {
// Else, if the item is already in the array, remove it
seletedItems.remove(Integer.valueOf(indexSelected));
}
}
}).setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String city = "";
for (int i = 0; i < seletedItems.size(); i++) {
if (i == seletedItems.size() - 1) {
city = city + items[Integer.parseInt(seletedItems.get(i).toString())];
} else {
city = city + items[Integer.parseInt(seletedItems.get(i).toString())] + ",";
}
}
btn_city_todisplay.setText(city);
dialog.dismiss();
}
}).setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// Your code when user clicked on Cancel
}
}).create();
dialog.show();
Use spinner method to show selected item
spinner.setSelection(position);
Here position is the last selected position