title: implement favorite button in listview android studio
body: <p>I am implementing favorite button in ListView trying from the last five days. I have created a ListView which gets the current textview text and saves it in the ArrayList (names) and if the data is already present then deletes it, works perfectly but when I close the app the ArrayList gets empty i wants to save the array list in  preferences.</p>
  
  <p>array declaration:</p>
  
  <pre><code>ArrayList&lt;String&gt; names = new ArrayList&lt;&gt;();
  </code></pre>
  
  <p>Below is the code</p>
  
  <pre><code>favoritebutton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick( View v) {
          if (!names.contains(textView_name.getText())){
              names.add((String) textView_name.getText());
  
              for (int i=0; i&lt;names.size(); i++) {
                  System.out.println(names.get(i));
                  favoritebutton.setBackgroundResource(R.drawable.fav_checked);
              }
          }
          else {
              System.out.println(textView_name.getText() + "   is already present in the Array at index " + names.indexOf(textView_name.getText()));
              int currentIndex = names.indexOf(textView_name.getText());
              names.remove(currentIndex);
              for (int i=0; i&lt;names.size(); i++) {
                  System.out.println(names.get(i));
                  favoritebutton.setBackgroundResource(R.drawable.star_off);
              }
          }
      }
  });
  </code></pre>