【问题标题】:Android listview refreshAndroid列表视图刷新
【发布时间】:2011-08-31 10:01:54
【问题描述】:

在我的应用程序中,当用户单击“添加”菜单按钮时,会出现一个列表视图,其中填充了从文本文件加载的项目。 所以现在用户可以在列表视图中再添加一项。将其添加到数组后,新项目被写入文本文件,但不会进入列表视图,因为我想通过将文件读取到数组然后用它填充 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部分被激活的原因。

我对这整件事不太确定。 可能声明放错地方了。

感谢任何帮助。

编辑:我已经设法解决了。解决方法如下。

【问题讨论】:

  • 你确定assignArr00 被填充了吗?你能发布那个代码吗?
  • 请也发布 logcat 输出
  • 在 eclipse 中没有 sd 卡,没有文本文件,这意味着 assignArr0 是空的,所以我无法在 eclipse 中测试它。在我的手机上,assignArr00 已填充,这是肯定的。在 else 部分用 Toast msg 检查。当然,它的大小为 1。

标签: android listview refresh


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-05-04
    • 2020-04-22
    • 2012-02-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-23
    • 2014-12-14
    • 1970-01-01
    相关资源
    最近更新 更多