Android ListView刷新

在我的应用程序中,当用户单击“添加”菜单按钮时,将出现一个列表视图,其中填充了从文本文件加载的项目.
因此,现在用户可以再添加一个项目到列表视图.将其添加到数组中后,新项目将写出到文本文件中,但不会进入列表视图,因为我想通过将文件读取到数组中然后使用它来填充ListView来做到这一点.问题是这不起作用.填充arrayadapter的方法位于onCreate(Bundle savedInstanceState){}方法中,但是添加操作在其外部,这是通过按菜单进行调用的.

public class Connection extends Activity {
ListView lv1;
ArrayList<String> assignArr0 = new ArrayList<String>();
ArrayList<String> assignArr00 = new ArrayList<String>();
ArrayAdapter<String> adapter1;
ArrayAdapter<String> 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<String>(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;
}
}

这是将行读入assignArr00后尝试执行的方式:

 if (fileisempty == false) //i set this boolean var at the beginning
          {
              adapter1.clear();
            //  adapter1 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00);
              //lv1.setAdapter(adapter1);
              //adapter1.notifyDataSetChanged();
          }
          else
          {
              adapter1 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00);
              lv1.setAdapter(adapter1);
              adapter1.notifyDataSetChanged();
              }
          }

最后一个代码的else部分导致强制关闭(不要忘记这是第一次运行,因此文本文件为空,这就是为什么else部分被激活的原因.

我不太确定这件事.
声明可能放在错误的位置.

任何帮助表示赞赏.

编辑:我设法解决了.解决方案可以在下面找到.

最佳答案
找到了解决方案:我不得不重新声明lv1 =(ListView)findViewById(R.id.ListView01);我没有这个背后的原因,但是花了我4个小时才找到答案.

if (fileisempty == false)
              {
                  adapter1.clear();
                  adapter1.notifyDataSetChanged();
                  lv1 = (ListView) findViewById(R.id.ListView01);
                  ArrayAdapter<String> adapter11;
                  adapter11 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00);
                  lv1.setAdapter(adapter11);
                  adapter11.notifyDataSetChanged();

              }
              else
              {
                  lv1 = (ListView) findViewById(R.id.ListView01);
                  ArrayAdapter<String> adapter11;
                  adapter11 = new ArrayAdapter<String>(Connection.this,R.layout.list_black_text,R.id.list_content, assignArr00);
                  lv1.setAdapter(adapter11);
                  adapter11.notifyDataSetChanged();
              }

转载注明原文:Android ListView刷新 - 代码日志