簡體   English   中英

永久刪除ListView項目Onclick

[英]Permanently Delete ListView Item Onclick

我設置了我的應用程序,以便當用戶單擊列表視圖中的項目時它消失。 但是,當我關閉活動或應用程序時,列表會刷新,其中包含所有項目。 單擊后是否可以永久刪除該項目?

這是我的代碼:

    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);

            }
        });
    }

}

您還應該將其從陣列列表中刪除。 因為列表視圖是從數組加載的,所以如果該列表視圖保持不變,則下次加載時的列表視圖也將保持不變。

您正在對Activity中的topMovies值進行硬編碼,因此,每當您打開活動時,此ArrayList的值都會被重置,並且即使刪除后,您也會一遍又一遍地看到同一項目。

在這種情況下,刪除過程將一直有效,直到您關閉活動或應用程序。

您在這里有兩個簡單的解決方案。

解決方案1

ArrayList需要的數據保存到設備中的sqlite數據庫中。 啟動應用程序/活動時,您將從DB中獲取數據並分配給ArrayList 當您從ListView刪除項目時,您會做兩件事。 一是從ArrayList刪除它(也可以在適配器上調用notifyDataSetChanged() ),二是從sqlite數據庫中刪除它。 這樣,當您啟動活動/應用程序時,每次都會有更新數據。

解決方案2 只是一個解決方案,或更像是解決方法

將所需的值存儲在ShardPreference中的ArrayList中,並執行與解決方案1相同的操作。

注意:您應該遵循第一個解決方案。 我添加第二個只是為了提供信息,也可以這樣做。

嘗試這種方式:

Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);

要么

修改ArrayList並在ArrayAdapter上調用notifyDataSetChanged()

arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();

嘗試從列表視圖中填充的數組中刪除正在單擊/刪除的項目,這將確保即使返回活動並退出列表視圖時,列表視圖也不會加載數據。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM