title: Android listview refresh
body: <p>In my app when user clicks on the ADD menu button, a listview appears populated with items that are loaded from a text file.
  So now user can add one more item to the listview. After adding it to an array, the new item is written out to the text file, but does not get into the listview, as i want to do it by reading the file to an array then populate the ListView with it. Problem is that this is not working. The arrayadapter populating is in the onCreate(Bundle savedInstanceState) {} method, but the adding is outside it as it is called by pressing a menu.</p>
  
  <pre><code>public class Connection extends Activity {
  ListView lv1;
  ArrayList&lt;String&gt; assignArr0 = new ArrayList&lt;String&gt;();
  ArrayList&lt;String&gt; assignArr00 = new ArrayList&lt;String&gt;();
  ArrayAdapter&lt;String&gt; adapter1;
  ArrayAdapter&lt;String&gt; adapter2;
  
  public void onCreate(Bundle savedInstanceState) {
  ...
  //filereading to assingArr0. Lines of the file are added to the listview.
  lv1 = (ListView) findViewById(R.id.ListView01);
  adapter1 = new ArrayAdapter&lt;String&gt;(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr0);
  lv1.setAdapter(adapter1);
  adapter1.notifyDataSetChanged();
  
  //now, if there were any lines in the text file, the ListView is populated with them.
  }
  
  public boolean onOptionsItemSelected(MenuItem item) {
      switch (item.getItemId()) {
      case R.id.Menu1:
  //this is where user adds an item to an array and item gets written out to the text file
  //and this is also the part where the listview should be cleared, the lines of the text file should be read into an array, and the listview should be populated with the items of that array.
  
  //The filereading is ok. Lines are read into assignArr00. Now what?
  
   break;
      }
      return true;
  }
  }
  </code></pre>
  
  <p>This is the way i tried to do it after lines are read into assignArr00: </p>
  
  <pre><code> if (fileisempty == false) //i set this boolean var at the beginning
            {
                adapter1.clear();
              //  adapter1 = new ArrayAdapter&lt;String&gt;(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00);
                //lv1.setAdapter(adapter1);
                //adapter1.notifyDataSetChanged();
            }
            else
            {
                adapter1 = new ArrayAdapter&lt;String&gt;(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00);
                lv1.setAdapter(adapter1);
                adapter1.notifyDataSetChanged();
                }
            }
  </code></pre>
  
  <p>The else part of this last code results in a force close (Don't forget that this is the first run, so the text file is empty, this is why the else part is activated.</p>
  
  <p>I am not very sure about this whole thing.
  Maybe the declarations are in a wrong place.</p>
  
  <p>Any help is appreciated.</p>
  
  <p>Edit: I have managed to solve it. Solution can be found below.</p>