Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Listview在适配器更改后保留旧项_Android_List_Listview_Listadapter - Fatal编程技术网

Android Listview在适配器更改后保留旧项

Android Listview在适配器更改后保留旧项,android,list,listview,listadapter,Android,List,Listview,Listadapter,我尝试在Android中更改Listview的内容。在那之后,我希望重新绘制列表。不幸的是,列表元素发生了变化,但旧项目显示在后台。如果新列表中的项目较少,则旧项目似乎仍然是列表的一部分,因为它们仍然显示,即使它们不可单击。如果我通过Android Taskmanager跳出应用程序,然后再次打开应用程序,列表将正确显示! 因此,我认为这一定是刷新的问题 我如何尝试实现它: 随着完整内容的更改,我将创建一个新的适配器,并将其传递给ListView。在我的代码中,“favorites”是我传递给适

我尝试在Android中更改Listview的内容。在那之后,我希望重新绘制列表。不幸的是,列表元素发生了变化,但旧项目显示在后台。如果新列表中的项目较少,则旧项目似乎仍然是列表的一部分,因为它们仍然显示,即使它们不可单击。如果我通过Android Taskmanager跳出应用程序,然后再次打开应用程序,列表将正确显示! 因此,我认为这一定是刷新的问题

我如何尝试实现它:

随着完整内容的更改,我将创建一个新的适配器,并将其传递给ListView。在我的代码中,“favorites”是我传递给适配器的新数组。我还试图清除列表并使其无效。所有这些都发生在UI线程中(AsyncTask的onPostExecute)

适配器的清除方法:

    public void clear(){
    items.clear();
    notifyDataSetChanged();
    }
正如你所见,我在stackoverflow上尝试了所有类似问题的解决方案。不幸的是什么都没用

提前感谢您提出的解决方案

编辑:

我还尝试在传递新适配器之前将适配器设置为null,但也没有效果。 .setAdapter(空)


另外,如果我通过单击编辑文本打开键盘,屏幕将刷新,列表将正确显示。

您是否尝试过
myList.setAdapter(null)

首先清除数据。我认为这是
最喜欢的
项目
或任何东西。并以这种方式通知适配器

items.clear();
((MyAdapter) myList.getAdapter()).notifyDataSetChanged();
MyAdapter newAdapter = new MyAdapter(context, favorites);
myList.setAdapter(newAdapter);
myList.invalidate();

我解决了这个问题,用一个比
列表视图
更重的布局填充列表下的剩余空间。这样,“额外”行就隐藏了

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/transparent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/main_list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

    <LinearLayout
        android:id="@+id/aux_layout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:background="@color/transparent"
        android:orientation="vertical" />

</LinearLayout>

我试过了,发现在清单文件的应用程序标记中,HardwareAcceleration必须设置为TRUE(也是默认值)。否则,listView将在键入期间在其背景中保留旧的不可单击项

android:hardwareAccelerated="true"

这是最终的解决方案吗?对我来说不起作用,我也有同样的问题。这对我来说非常有效,在控件收缩后,Android似乎没有正确清理“垃圾”。所以无论是在它里面,但现在在它外面的东西,都会停留在那里,直到被其他东西覆盖。扩展容器做这项工作这对我来说非常有效。所有其他的都不起作用。谢谢你,伙计
android:hardwareAccelerated="true"