IntoTheDeep
IntoTheDeep

Reputation: 4118

Android queried adapter reset

Is it possible to reset my adapter to original state, without make a new query?

For example

I have JSONArray, that I cut some of records to show selected in ListView, but I want to reset data and show all records again. Does that possible?

private JSONArray modifyJsonArray(JSONArray array, final Integer param) throws JSONException {
    List<JSONObject> jsons = new ArrayList<JSONObject>();
    for (int i = 0; i < array.length(); i++) {
        Integer statusId = Integer.parseInt(array.getJSONObject(i).getString("statusId"));
        if (statusId == param){
            jsons.add(array.getJSONObject(i));
        }
    }
    return new JSONArray(jsons);
}


private void selectStatus(JSONArray arr, Integer param) throws JSONException {
    myArr= modifyJsonArray(arr, param);
    adapter.notifyDataSetChanged();
}

I have a spinner with all statusId and option "All". When all is selected I need to show all records.I have choose status id from simple spinner, and just call selectStatus(myArr, statusId) The problem is when I remove records from array, I cannot restore it back.

Upvotes: 0

Views: 50

Answers (1)

Vickyexpert
Vickyexpert

Reputation: 3171

Now read below instruction and follow it for your solution,

First Create on Variable Outside methods as like below,

 List<JSONObject> mainJsons = new ArrayList<JSONObject>();

Now modify your method for modifyJsonArray as below

  private JSONArray modifyJsonArray(JSONArray array, final Integer param) throws JSONException 
  {
      List<JSONObject> jsons = new ArrayList<JSONObject>();
      mainJsons = new ArrayList<JSONObject>();

      for (int i = 0; i < array.length(); i++) 
      {
          Integer statusId = Integer.parseInt(array.getJSONObject(i).getString("statusId"));

          mainJsons.add(array.getJSONObject(i));

          if (statusId == param)
          {
              jsons.add(array.getJSONObject(i));
          }
      }

     return new JSONArray(jsons);
  }

Now create one method for reset main array as below

  private void resetStatus(JSONArray arr) throws JSONException 
  {
      if(arr != null && arr. size() > 0)
      {
           myArr = arr;
           adapter.notifyDataSetChanged();
      }
  }

Now call this method as below where you want to reset listviw

  resetStatus(new JSONArray(mainJsons));

Upvotes: 1

Related Questions