java - 永久删除 ListView 项 Onclick

标签 java android listview

我设置了我的应用程序,以便当用户单击 ListView 中的项目时它会消失。但是,当我关闭 Activity 或应用程序时,列表会缓慢刷新并包含所有项目。有没有办法让我在点击后永久删除该项目?

这是我的代码:

    public class Movies extends AppCompatActivity {

    ArrayAdapter<String> arrayAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_movies);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        final ListView moviesListView = (ListView)findViewById(R.id.moviesListView);

        final ArrayList<String> topMovies = new ArrayList<String>(asList("1. The Shawshank Redemption", "2. The Godfather", "3. The Godfather: Part 2",
                "4. The Dark Knight", "5. Pulp Fiction","6. Schlinder's List","7. 12 Angry Men","8. The Lord of the Rings: The Return of the King",
                "9. The Good, the Bad and the Ugly","10. Fight Club","11. The Lord of the Rings: The Fellowship of the Ring","12. Star Wars: Episode V - The Empire Strikes Back",
                "13. Forrest Gump","14. Inception","15. One Flew Over the Cuckoo's Nest","16. The Lord of the Rings: The Two Towers","17. Goodfellas",
                "18. The Matrix","19. Seven Samurai","20. Star Wars: Episode IV - A New Hope","21. City of God","22. Se7en","23. The Silence of the Lambs",
                "24. The Usual Suspects","25. It's a Wonderful Life","26. Life is Beautiful","27. Leon: The Professional","28. Once Upon a Time in the West",
                "29. Spirited Away","30. Interstellar","31. Saving Private Ryan","32. Casablanca","33. American History X","34. Psycho","35. City Lights","36. Raiders of the Lost Ark",
                "37. Rear Window","38. The Intouchables","39. Modern Times","40. The Green Mile","41. Terminator 2: Judgement Day","42. The Pianist","43. The Departed",
                "44. Whiplash","45. Back to the Future","46. Memento","47. Gladiator","48. Apocolypse Now","49. Dr. Strangelove","50. The Prestige"));

        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, topMovies);

        moviesListView.setAdapter(arrayAdapter);

        final MediaPlayer mPlayer = MediaPlayer.create(this, R.raw.pindrop);



        moviesListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                String toRemove = arrayAdapter.getItem(position);
                arrayAdapter.remove(toRemove);

            }
        });
    }

}

最佳答案

您还应该将其从数组列表中删除。因为您的 ListView 是从您的数组中加载的,所以如果该 ListView 保持不变,则下一次加载的 ListView 也将保持不变。

关于java - 永久删除 ListView 项 Onclick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35340564/

相关文章:

android - 如何在 Android 中刷新 ListView

java - 为什么这段代码在我的系统上运行良好并在 HackerRank 中抛出 EmptyStackException

android - 无数据丢失的房间数据库架构更新

android - 引导用户点击 ListView 中的项目进行新 Activity

android - Windows 和 BlackBerry 图标大小和像素密度

android - '错误 : Ambient Vertex Buffer overflow!!使用 304,总共 260' Android ViewPropertyAnimator

c - LVM_GETITEM 和 LVM_GETITEMTEXT 有什么区别?

javafx:如何在 TreeView 中隐藏 "drop down arrow"?

java - 如何排序和保留 web 服务的结果?

Java:部署到服务器后FileNotFound