I want to implement one Spinner where spinner items will come from server side now I want items come at first time on spinner after that whenever I will reopen that app or spinner the items will not come from server side, items should be there. Below is my custom spinner where country name is coming from server side.
// for country spinner
StringRequest sR = new StringRequest(Request.Method.POST, country_url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
ArrayList<String> arr = new ArrayList<String>(Arrays.asList(response.trim().split(",")));
for (int i = 0; i < arr.size(); i++) {
if (arr.get(i).equals("India")) {
arr.remove(i);
}
}
arr.add(0, "Select Country");
arr.add(1, "India");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EditDetails7.this, android.R.layout.simple_list_item_1, arr);
spinCountry.setAdapter(adapter);
for(int i=0; i < adapter.getCount(); i++) {
if (country.trim().equals(adapter.getItem(i).toString())) {
spinCountry.setSelection(i);
break;
}
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(EditDetails7.this, error.toString(), Toast.LENGTH_LONG).show();
}
});
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(sR);
You need to save the service response in local storage. To do that you can do one of 2 things :
1) You can save data in database 2) you can save data in shared preference
It all depends upon your requirements. If the data is huge I would prefer to use data base else shared preference is the best choice. Hope that helps.
Below are the links that may help you For SharedPreference
https://www.androidhive.info/2017/07/android-implementing-preferences-settings-screen/
For database
https://www.androidhive.info/2011/11/android-sqlite-database-tutorial/