user6734679 user6734679 - 5 months ago 67
Android Question

How to load spinner with local data once data retrieve from server android

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>() {
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.add(0, "Select Country");
arr.add(1, "India");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(EditDetails7.this, android.R.layout.simple_list_item_1, arr);
for(int i=0; i < adapter.getCount(); i++) {
if (country.trim().equals(adapter.getItem(i).toString())) {
}, new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
Toast.makeText(EditDetails7.this, error.toString(), Toast.LENGTH_LONG).show();
RequestQueue requestQueue = Volley.newRequestQueue(this);

Answer Source

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

For database
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download