title: Android - ArrayAdapter duplicates ListView items
body: <p>I have a <code>ListView</code> widget (positioned below other UI elements) that is part of an <code>Activity</code> that does not extend <code>ListActivity</code>. I am trying to write a basic feature that adds extra items to the <code>ListView</code> upon clicking a <code>Button</code>. This data is obtained from an <code>EditText</code>. I define the <code>Adapter</code>, an <code>ArrayAdapter</code> in my case, for the <code>ListView</code> in <code>onCreate()</code> and set the adapter for the <code>ListView</code> using <code>setListAdapter()</code>. However when I try to add an extra item to the <code>ListView</code> two items are added in the place of one (an item and its duplicate). More detail on this below. </p>
  
  <p>The function that responds to the <code>Button</code> click and attempts to add an extra element before calling <code>notifyDataSetChanged()</code> is as:    </p>
  
  <pre><code>public void saveButton(View view){
  
       EditText editText = (EditText) findViewById(R.id.edit_text);
       String data = editText.getText().toString();
       // duplication caused by this line
          b.add(data);
       ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();
       adapter.add((String)data);
       adapter.notifyDataSetChanged();
  }
  </code></pre>
  
  <p>When I do not include this line: </p>
  
  <blockquote>
    <p>b.add(data);</p>
  </blockquote>
  
  <p><strong>a single item is added to the list which is expected behaviour as <code>adapter.add(Object)</code> appends an extra item to the list. However even queerer, is that when I do not include <code>adapter.add(Object)</code> a list item is still rendered in the list.</strong> </p>
  
  <pre><code> public void saveButton(View view){
  
       EditText editText = (EditText) findViewById(R.id.edit_text);
       String data = editText.getText().toString();
       b.add(data);
       ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();
       // not calling adapter.add() still includes an item in the list.
       // adapter.add((String)data);
       adapter.notifyDataSetChanged();
  }
  </code></pre>
  
  <p><strong>After a bit of debugging I found that this is owing to the addition of a data item to the <code>ArrayList</code>, i.e. <code>b.add(data)</code> where b is an ArrayList of Strings, causes an item to be included in the <code>ListView</code> despite not calling adapter.add(Object)`.</strong>  </p>
  
  <p>Q1. Why is this happening ? </p>
  
  <p>Q2. As an extension, does this mean that the <code>ArrayList</code> is 'coupled' to my <code>ArrayAdapter</code> (as it was constructed using the ArrayList as a argument) implying that I needn't call adapter methods like add() or clear() to manipulate my <code>ListView</code> ?  </p>
  
  <p>Here is the full code for my Activity as well:</p>
  
  <pre><code>public class MainActivity extends Activity {
  
  ListView listView;
  ArrayList&lt;String&gt; b = new ArrayList&lt;String&gt;();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      b.add("Liquorice");
      b.add("Dairy Milk");
      b.add("Rice Crispies");
      b.add("Orange-Mint");
      b.add("After-Tens");
  
  
      listView = (ListView) findViewById(R.id.list_view);
      ArrayAdapter adapter = new ArrayAdapter&lt;String&gt;(getApplicationContext(),R.layout.layout_file,b);
      listView.setAdapter(adapter);
  
  }
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.main, menu);
      return true;
  }
  
  public void saveButton(View view){
  
       EditText editText = (EditText) findViewById(R.id.edit_text);
       String data = editText.getText().toString();
       b.add(data);
       ArrayAdapter adapter = (ArrayAdapter) listView.getAdapter();
       adapter.add((String)data);
       adapter.notifyDataSetChanged();
  }
  
  }
  </code></pre>
  
  <p>I also had a look around other SO posts, but they have not given me the answers I was looking for. </p>
  
  <p><a href="https://stackoverflow.com/questions/19079400/arrayadapter-in-android-to-create-simple-listview">ArrayAdapter in android to create simple listview</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3476723/why-cant-one-add-remove-items-from-an-arrayadapter">Why can&#39;t one add/remove items from an ArrayAdapter?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/8261376/duplicated-entries-in-listview">Duplicated entries in ListView</a></p>
  
  <p>Any direction or an explanation on this would be most appreciated. Also thank you very much for your time. </p>