title: Unable to clear data in ArrayAdapter by using adapterName.clear()
body: <p>I'm using <code>PreferenceFragment</code> and I want that every time I change the value of a <code>PreferenceScreen's widget</code> the results should be displayed according to that in the <code>ListView</code>. Everything is working fine except when I change the value of any <code>PreferenceScreen's widget</code> instead of showing me a new list in the <code>ListView</code> it's adding the values in that same list. Apparently, <code>adapter.clear()</code> is not working.</p>
  
  <p>Can anyone suggest how to make it work?</p>
  
  <p>Here is my MainActivity:</p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements 
  LoaderManager.LoaderCallbacks&lt;String&gt;, 
  SharedPreferences.OnSharedPreferenceChangeListener{
  private static final String TAG = "MainActivity";
  
  private static final int LOADER_INIT_KEY = 0;
  
  EditText titleTextView;
  Button searchButton;
  ListView listView;
  
  
  String query = "";
  private ArrayList&lt;String&gt; authorsList = new ArrayList&lt;&gt;();
  private ArrayList&lt;BooksDetails&gt; booksDetails = new ArrayList&lt;&gt;();
  private String title = null, id = null;
  
  private CustomAdapter adapter;
  
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main_layout);
  
      titleTextView = findViewById(R.id.author_textView);
      searchButton = findViewById(R.id.search);
      listView = findViewById(R.id.list_view);
  
      /**
       * this declaration is needed
       */
       adapter = new CustomAdapter(this, new ArrayList&lt;BooksDetails&gt;());
  
       final ListView listView = findViewById(R.id.list_view_in_main_layout);
  
       listView.setAdapter(adapter);
  
       // getting instance of SharedPreference and attaching listener to it
      SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
      //  any change in the SharedPreference will get notified
      //  if user tweaks any settings
      sharedPreferences.registerOnSharedPreferenceChangeListener(this);
  
      //  Search button
      searchButton.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
  
              //  getting value from textView
              query = titleTextView.getText().toString();
  
              Log.d(TAG, "onClick: "+query);
  
              //  clearing the adapter
              adapter.clear();
  
              //  checking the network connection
              ConnectivityManager connectivityManager = (ConnectivityManager)getSystemService(CONNECTIVITY_SERVICE);
              assert connectivityManager != null;
              NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
  
              if(networkInfo != null &amp;&amp; networkInfo.isConnected()) {
                  if(query.length() != 0) {
  
                      getSupportLoaderManager().initLoader(LOADER_INIT_KEY, null, MainActivity.this);
                      closeKeyboard();
                      Log.d(TAG, "onClick: ");
                  }
                  else
                      Toast.makeText(MainActivity.this, "Enter a title", Toast.LENGTH_SHORT).show();
              }
              else{
                  Toast.makeText(MainActivity.this, "Not connected to internet", Toast.LENGTH_SHORT).show();
              }
          }
      });
  }
  
  private void closeKeyboard(){
      //  hiding keyboard after the search
      InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
  
      View focusedView = this.getCurrentFocus();
      if(focusedView != null) {
          try {
              assert inputMethodManager != null;
  
          inputMethodManager.hideSoftInputFromWindow(focusedView.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
          }catch (AssertionError e){
              Log.e(TAG, "closeKeyboard: Assertion error is thrown inputMethodManager is null", e);
          }
      }
  }
  
  @NonNull
  @Override
  public android.support.v4.content.Loader onCreateLoader(int id, @Nullable Bundle args) {
  
      Log.d(TAG, "onCreateLoader: ");
      //  getting the SharedPreference which contains all the values of the Preference
  
      //  SharedPreference for maxResults
      SharedPreferences maxResultsSharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
      String maxResults = maxResultsSharedPreferences.getString(getString(R.string.settings_default_result_key),
              getString(R.string.settings_default_value_of_result));
  
      //  SharedPreference for OrderBy
      SharedPreferences orderBySharedPreference = PreferenceManager
      .getDefaultSharedPreferences(this);
  
      String orderByValue = orderBySharedPreference.getString(getString(R.string.settings_order_by_key),
              getString(R.string.settings_order_by_default));
  
  
      //  passing the minResults with query
      return new CustomLoader(this, query, maxResults, orderByValue);
  }
  
  
  @Override
  public void onLoadFinished(@NonNull android.support.v4.content.Loader loader, String s) {
      Log.d(TAG, "onLoadFinished: ");
  
      //  clearing the adapter
      adapter.clear();
  
      //  after getting the raw json we are parsing it here
      try {
          JSONObject rootObject = new JSONObject(s);
          JSONArray itemsArray = rootObject.getJSONArray("items");
  
          for(int i = 0;i &lt; itemsArray.length();i++){
              //  getting each object in the array itemsArray
              JSONObject booksInfo = itemsArray.getJSONObject(i);
  
              //  getting book id
              id = booksInfo.getString("id");
  
              //  getting the volumeInfo object
              JSONObject volumeInfo = booksInfo.getJSONObject("volumeInfo");
  
              //  getting title of the book
              title = volumeInfo.getString("title");
              //  getting the author
  
              JSONArray author = volumeInfo.getJSONArray("authors");
  
              //  getting all the members of author array
              for(int j = 0;j &lt; author.length();j++){
                  authorsList.add(author.getString(j));
              }
  
              if(title == null) {
                  //  if no title matches
                  titleTextView.setText("No title matches");
              }
  
              //   adding details in the ArrayList bookDetails
              booksDetails.add(new BooksDetails(id, title, authorsList.get(0)));
  
          }
  
          adapter.addAll(booksDetails);
      }
      catch(Exception e){
          Log.e(TAG, "onLoadFinished: exception in json parsing", e);
      }
  }
  
  @Override
  public void onLoaderReset(@NonNull android.support.v4.content.Loader loader) {
      Log.d(TAG, "onLoaderReset: ");
  
      //  clearing the adapter
      adapter.clear();
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      getMenuInflater().inflate(R.menu.settings_menu, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      if(item.getItemId() == R.id.action_settings){
          Intent intent = new Intent(this, SettingsActivity.class);
          startActivity(intent);
      }
      return super.onOptionsItemSelected(item);
  }
  
  @Override
  public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
      Log.d(TAG, "onSharedPreferenceChanged: ");
  
      if(key.equals(getString(R.string.settings_order_by_key)) || key.equals(getString(R.string.settings_default_result_key))){
  
          //  clearing the adapter
          adapter.clear();
  
          getSupportLoaderManager().restartLoader(LOADER_INIT_KEY, null, this);
      }
  }
  
  @Override
  protected void onRestart() {
      super.onRestart();
  
      adapter.clear();
  }
  </code></pre>
  
  <p>}</p>