简体   繁体   English

适配器更新时列表视图中的灰色空间

[英]Grey space in listview when adapter is updated

So i have a listview and adapter set up. 所以我有一个listview和适配器设置。 When i add an item trough adapter.add() in the oncreate() method everything is fine. 当我在oncreate()方法中添加一个槽槽adapter.add()时,一切都很好。 But when i add it trough a listener it creates this weird grey space as if the items are underneath the other items: 但是,当我通过一个侦听器添加它时,它会创建此奇怪的灰色空间,好像这些项目在其他项目之下:

在此处输入图片说明

This is the code that adds the items: 这是添加项目的代码:

                ArrayList<Task> newTasks = new ArrayList<>(Arrays.asList(parser.ParseTasksJson(result)));

                for (int i =0; i < 3;i++)
                {
                    Task task = new Task(i, "Item" + i, 0);
                    tasks.add(task);
                }

                for(Task t : newTasks)
                {

                    adapter.add(new Task(t.getTask_id(), t.getTask_title(), t.getTask_done()));
                }
                adapter.add(new Task(99,"Task after forloop", 0));

                adapter.notifyDataSetChanged();`enter code here`

And this is my layout: 这是我的布局:

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <include layout="@layout/toolbar_home"/>

    <ListView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/listview">
    </ListView>
</LinearLayout>

EDIT: I was not clear enough. 编辑:我还不够清楚。 The real problem is in the fact that the items from the parser dont get shown in the listview although they are added to the adapter. 真正的问题在于,尽管解析器中的项目已添加到适配器中,但它们并未显示在列表视图中。 SEE updated code. 查看更新的代码。

Your code is working properly, however the problem is in this line: 您的代码正常运行,但是问题出在这一行:

adapter.add(new Task(t.getTask_id(), t.getTask_title(), t.getTask_done()));

Make sure that the values are correct, to me it seems that your data does not contain a valid title, and your layout items have a height of wrap_content . 确保值正确,对我来说,您的数据似乎不包含有效的标题,并且布局项的高度为wrap_content That being said, the items are added, but not high enough since they don't have any content. 话虽如此,这些项已添加,但由于它们没有任何内容而不够高。

That's why you see that weird grey space, they are actually several items, but not big enough to see. 这就是为什么您会看到奇怪的灰色空间,它们实际上是几个项目,但不足以看到。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM