Android的列表视图刷新 [英] Android listview refresh

查看:180
本文介绍了Android的列表视图刷新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序,当用户单击添加菜单按钮,一个ListView出现填充的是从一个文本文件中加载的项目。 所以现在用户可以添加更多的项目到列表视图。将它添加到一个数组后,该新项目将写入到文本文件,但没有进入列表视图,因为我想通过读取文件到一个数组,然后填充ListView与它这样做。问题是,这是行不通的。一个ArrayAdapter填充是在所述的onCreate(捆绑savedInstanceState){}方法,但加法是在它之外,因为它是由称为pressing一个菜单。

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

这是我试图做到这一点线被读入assignArr00后的方式:

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

在一个强制关闭这最后code结果的其他部分(不要忘了,这是第一次运行,因此文本文件是空的,这就是为什么其他部分被激活。

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.

任何帮助是AP preciated。

Any help is appreciated.

编辑:我设法解决这个问题。解决方案可以在下面找到。

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

推荐答案

找到了解决办法:我不得不重新定义 LV1 =(ListView控件)findViewById(R.id.ListView01); 我不知道这背后的原因,但我花了4个小时发现这一点。

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

这篇关于Android的列表视图刷新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆