Start spinner in different activates by one method without copying the code
-
There are two activates where the spinner needs to be filled with the same data. Fillm:
private void loadCategories() {
final ProgressDialog pd = new ProgressDialog(this); pd.setCancelable(false); pd.show(); StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_GET_CATEGORIES, new Response.Listener<String>() { @Override public void onResponse(String response) { try { JSONObject res = new JSONObject(response); JSONObject meta = res.getJSONObject("meta"); // Auth OK if (AppController.getInstance().metaCheck(meta)) { JSONObject data = res.getJSONObject("data"); JSONArray categories = data.getJSONArray("categories"); for (int i = 0; i < categories.length(); i++) { JSONObject catObj = (JSONObject) categories.get(i); Bundle b = new Bundle(); b.putString("id", catObj.getString("id_fc")); b.putString("name", catObj.getString("name")); b.putInt("is_standard", catObj.getInt("standard")); spinnerArray.add(b); } adapter.notifyDataSetChanged(); } pd.dismiss(); } catch (JSONException e) { // JSON error e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { AppController.getInstance().volleyOnErrorResponse(error); pd.dismiss(); } }) { @Override protected Map<String, String> getParams() { // Posting parameters to login url Map<String, String> params = new HashMap<>(); if (sm == null) { sm = new SessionManager(getApplicationContext()); } String accessToken = sm.getAccessToken(); params.put("access_token", accessToken); return params; } @Override public Map<String, String> getHeaders() throws AuthFailureError { //HTTP Auth return AppController.getInstance().headerAUTH(); } }; // Adding request to request queue AppController.getInstance().addToRequestQueue(strReq);
}
For each activate, copy the same code. And then for each change, we need to update the code in both functions.
The thing is, I have a caste view for the spinner. And there's buttons, like, "stop the category." So I don't want to copy a huge code into every new activism.The question is how to initial the spinner in all of us activate without copying the same code?
-
Do one class activitis and put your method in it from the question by changing its access moderator.
protected
insteadprivate
♪ The rest of you activate this new class, not the usual activism. So you'll have access to this method in every activist and you need to change it in one place. And the code in your activities will be smaller.