Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid Listview keeps old items after adapter changes
    primarykey
    data
    text
    <p>I try to change the content of a Listview in Android. After that I want the list to be redrawn. Unfortunately the list elements change, but the old items are displayed in the background. If there are less items in the new list it seems like the old items are still part of the list, as they are still displayed, even though they are not clickable. If I jump out of the app via the Android Taskmanager and afterwards open the app again, the list gets displayed correctly! Therefore I think it must be a problem with refresh. </p> <p>How do I try to achieve it:</p> <p>As the complete content changes I create a new adapter that I pass to the ListView. In my code "favorites" is the new Array that I pass to the adapter. I also try to clear the list and invalidate it. all of this happens in the UI thread (onPostExecute of AsyncTask)</p> <pre><code> MyAdapter newAdapter = new MyAdapter( context, favorites); MyAdapter current_adapter = (MyAdapter) myList.getAdapter(); if((current_adapter!=null)){ current_adapter.clear();} myList.setAdapter(null); //inserted this because of answers, but with no effect. myList.setAdapter(newAdapter); myList.invalidateViews(); </code></pre> <p>The clear method of the adapter:</p> <pre><code> public void clear(){ items.clear(); notifyDataSetChanged(); } </code></pre> <p>As you can see I tried all of the solutions to similar problems her on stackoverflow. Unfortunately nothing worked...</p> <p>Thanks in advance for any ideas how to solve this.</p> <p>Edit:</p> <p>I have also tried to set the adapter null before passing the new one, also with no effect. .setAdapter(null);</p> <p>Also if I open the keyboard by clicking in a editText, the screen refreshs and the list is displayed correctly.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload