Android 列表视图刷新

发布于 2024-11-09 10:16:40 字数 2302 浏览 0 评论 0原文

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

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;
}
}

这是我在将行读入 allocateArr00 后尝试执行的方法:

 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 部分已激活。

我对整个事情不太确定。 也许声明放错了地方。

任何帮助表示赞赏。

编辑:我已经设法解决它。解决方案可以在下面找到。

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.

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;
}
}

This is the way i tried to do it after lines are read into 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();
              }
          }

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.

I am not very sure about this whole thing.
Maybe the declarations are in a wrong place.

Any help is appreciated.

Edit: I have managed to solve it. Solution can be found below.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

冷夜 2024-11-16 10:16:40

找到解决方案:我必须重新声明 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();
              }

Found the solution: I had to redeclare lv1 = (ListView) findViewById(R.id.ListView01); I don't the reason behind this, but it cost me 4 hours to find this out.

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();
              }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文