Android ListView子项可能未更新

时间:2014-04-11 08:53:13

标签: android listview

我正在使用ListView。我在开头添加了10个元素,并在点击时添加元素。我的问题是子计数没有更新。

mItems = new ArrayList<String>();
mItems.add("Diary of a Wimpy Kid 6: Cabin Fever");
...
mItems.add("Death Comes to Pemberley");
// 10 items
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, mItems);

mListView = (RefreshableListView) findViewById(R.id.listview);
mListView.setAdapter(adapter);

之后,在某些时候,我有:

mItems.add(result);
invalidateViews();

显示新项目,但getChildCount()仍然返回10。 我需要该调用来返回正确的值,因为在某些时候我需要了解是否使用这个显示最后一个元素:

if (this.getLastVisiblePosition() == this.getChildCount())

在该检查中,我this.getLastVisiblePosition() = 11且this.getChildCount() = 10。 最后显示的子位置如何大于子计数?

添加多个元素,我最终得到15到10,所以它不是一次性出现。

任何想法都将不胜感激!

2 个答案:

答案 0 :(得分:0)

将项目添加到适配器,然后使用adapter.notifyDataSetChanged()

答案 1 :(得分:0)

我最终找到了这个问题的答案here。我在getChildCount()之间做了一个coufusion,它返回了视图组的子视图数(显示的视图数),它总是10,因为一次显示了10个元素getCount() 返回适配器的实际大小(列表中元素的总数)。

一旦我被允许,我会将此答案标记为正确答案。