gpt4 book ai didi

java - 永久删除 ListView 项 Onclick

转载 作者:行者123 更新时间:2023-11-29 20:08:21 26 4
gpt4 key购买 nike

我设置了我的应用程序,以便当用户单击 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com