简体   繁体   中英

Grey space in listview when adapter is updated

So i have a listview and adapter set up. When i add an item trough adapter.add() in the oncreate() method everything is fine. 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 . 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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