Android listview适配器问题

发布于 2024-11-09 01:32:00 字数 916 浏览 5 评论 0原文

在我的活动中,有一个包含文本文件行的列表视图。为了填充此列表视图,我在 onCreate(Bundle savingInstanceState) 方法中创建了一个数组适配器。如果文件中有行,列表视图将填充项目。 arrayadapter 部分如下所示:

   final ListView lv1 = (ListView) findViewById(R.id.ListView01);
 ArrayAdapter<String> adapter1;
    adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,R.id.list_content, assignArr0);
    lv1.setAdapter(adapter1);
    adapter1.notifyDataSetChanged();

然后通过单击 Menu item1 (所以现在我们在 onCreate(Bundle savingInstanceStat) 方法之外,我在文本文件中写入一行。现在一切正常。如果我退出并重新打开应用程序,新的行出现在列表视图中,但添加新行后列表视图不会刷新,因此在“case R.id.Menu1:”部分的末尾,

所以在向文件添加一行后我想刷新列表视图。阅读行从文件到数组,然后用该数组填充列表视图,即使我将 ArrayAdapteradapter1; 行放在 onCreate() 方法之外。声明了其他数组和变量。

它看不到列表视图(lv1 无法解析)。 如果我定义其他变量所在的 ListView (ListView lv1;),则程序的整个部分将无法工作。

我还需要在填充之前通过 adapter1.clear(); 或 sg 清空列表视图(适配器?)。 欢迎任何想法。

In my activity there is a listview containing lines of a text file. To populate this listview i created an arrayadapter in the onCreate(Bundle savedInstanceState) method. The listview is populated with items if there are lines in the file. The arrayadapter part looks like this:

   final ListView lv1 = (ListView) findViewById(R.id.ListView01);
 ArrayAdapter<String> adapter1;
    adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,R.id.list_content, assignArr0);
    lv1.setAdapter(adapter1);
    adapter1.notifyDataSetChanged();

Then by clicking on Menu item1 (so now we are outside the onCreate(Bundle savedInstanceStat) method i write one line in the text file. For now everything works fine. If i exit and reopen the app the new line appears in the listview. But the listview is not refreshed after adding the new line, so at the end of the " case R.id.Menu1:" part.

So after adding one line to the file I want to refresh the listview by reading the lines from the file to an array then populating the listview with that array. Problem is that i cannot do that, even if i put the ArrayAdapter<String> adapter1; line outside the onCreate() method where the other arrays and variables are declared.

It does not see the listview (lv1 cannot be resolved).
If i define the ListView where the other variables are (ListView lv1;) this whole part of the program is not working.

I also need to empty the listview (the adapter?) by adapter1.clear(); or sg before populating it.
Any ideas are welcome.

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

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

发布评论

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

评论(1

世态炎凉 2024-11-16 01:32:00

如果您需要这些变量是全局变量,则应将它们声明为

private final ArrayList<String> assignArr0 = new ArrayList<String>();
private ListView lv1;
private ArrayAdapter<String> adapter1;

@Override
public void onCreate(Bundle icicle)
{
    lv1 = (ListView) findViewById(R.id.ListView01);
    adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,
        R.id.list_content, assignArr0);
    lv1.setAdapter(adapter1);
}

您应该仅在 Activity 的 onCreate 方法内将适配器设置为 ListView 一次,并且对列表(ListAdapter 中使用的数组)进行更改后,应调用 notifiyDatasetChanged

假设您有一种方法可以添加尚未包含在列表中的最近阅读的行。将行添加到 ListAdapter 中使用的 ArrayList 后,您应该调用 notifyDatasetChanged 方法,以可视化 中的新项目ListView

private void addLinesToList(final String... newLines)
{
    assignArr0.addAll(Arrays.asList(newLines));
    adapter1.notifyDataSetChanged();
}

如果每次更改时都读取整个文件,并从顶部填充它(而不仅仅是插入新行),则必须首先删除列表的所有元素,因此addLinesToList 方法应该是:

assignArr0.clear();

If you need those variables to be global, you should declare them as

private final ArrayList<String> assignArr0 = new ArrayList<String>();
private ListView lv1;
private ArrayAdapter<String> adapter1;

@Override
public void onCreate(Bundle icicle)
{
    lv1 = (ListView) findViewById(R.id.ListView01);
    adapter1 = new ArrayAdapter<String>(Main.this,R.layout.list_black_text,
        R.id.list_content, assignArr0);
    lv1.setAdapter(adapter1);
}

You should set the adapter to the ListView only one time, inside the onCreate method of your activity, and the notifiyDatasetChanged should be called after you made changes to the list (the array used in your ListAdapter).

Say you have a method to add the recently read lines not yet included in your list. After adding the lines to the ArrayList used in the ListAdapter, you should call the notifyDatasetChanged method, to visualize the new items in the ListView:

private void addLinesToList(final String... newLines)
{
    assignArr0.addAll(Arrays.asList(newLines));
    adapter1.notifyDataSetChanged();
}

If you are reading the whole file every time it changes, and populate it from top (not just inserting the new lines), you have to first remove all the elements of the list, so the first line of the addLinesToList method should be:

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