我设置了自己的应用,以便当用户点击列表视图中的某个项目时,它就会消失。但是,当我关闭活动或应用程序时,列表会刷新所有项目。有没有办法让我在点击后永久删除该项目?
这是我的代码:
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);
}
});
}
}
答案 0 :(得分:1)
您还应该从阵列列表中删除它。因为你的列表视图从你的数组中加载,所以如果它保持不变,下一次加载的列表视图也将保持不变。
答案 1 :(得分:1)
您在活动中对topMovies
的值进行了硬编码,因此每当您打开活动时,此ArrayList
中的值都会重置,反过来您即使在重复后也会看到相同的项目删除它。
在此方案中,删除过程会一直有效,直到您关闭活动或应用程序。
这里有两个简单的解决方案。
解决方案1
将所需数据保存在设备的ArrayList
至sqlite
数据库中。启动应用/活动时,您可以从数据库中获取数据并分配给您的ArrayList
。当您从ListView
中删除该项时,您会做两件事。一,将其从ArrayList
(也适用于适配器上的notifyDataSetChanged()
)中删除,然后将其从sqlite
数据库中删除。这样,当您启动活动/应用程序时,您每次都会获得更新数据。
解决方案2 只是解决方案或更像解决方案
将ArrayList
中所需的值存储在ShardPreference
中,并执行与解决方案1 相同的操作。
注意:您应该遵循第一个解决方案。我已经添加了第二个,只是为了提供信息,这也可以做到。
答案 2 :(得分:0)
尝试这种方式:
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
或强>
修改ArrayList并在ArrayAdapter上调用notifyDataSetChanged()
。
arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
答案 3 :(得分:0)
尝试从列表视图中填充的阵列中删除正在点击/删除的项目,这反过来将确保即使回到您的活动并退出,您的列表视图也不会加载数据它