简体   繁体   English

如何从ArrayList中删除条目?

[英]How to delete entries from an ArrayList?

I'm trying to make a list of things (specifically Strings) that people have favorited, if they click 'add to Favorites' it executes this: 我正在尝试列出人们喜欢的事物(特别是字符串)的列表,如果他们单击“添加到收藏夹”,它将执行以下操作:

Class declaration: 类声明:

ArrayList<String> favoritesArray = new ArrayList<String>();
String[] factoids;
int counter;

onCreateBundle below: 下面的onCreateBundle:

Button favoritesButton = (Button) findViewById(R.id.button1);
Resources res = getResources();
factoids = res.getStringArray(R.array.factsArray);

favoritesButton ActionListener below: 最喜欢的按钮ActionListener如下:

favoritesArray.add(factoid[counter]);

And then the ListView in the Favorites XML displays the new text, however, I'm stumped on what to do when they un -favourite the string. 然后,收藏夹XML中的ListView将显示新文本,但是,当他们喜欢该字符串时,我很困惑。 I would like the added Favorite text to be removed from the favoritesArray . 我想从删除添加喜欢的文字favoritesArray How would I do this? 我该怎么做?

There is a method provided by ArrayList called .remove("your string here") you can use. 您可以使用ArrayList提供的一种名为.remove("your string here")方法。

For example, arrListFav.remove("A") will find and remove the first occurrence of the string "A" in your array list. 例如, arrListFav.remove("A")将查找并删除数组列表中字符串"A"的第一个出现。

You can simple use (remove) as shown bellow: for example you want to remove entity1 in the arraylist.. 您可以如下所示简单使用(删除):例如,您要删除arraylist中的entity1。

favoritesArray.remove("entity1"); favoriteArray.remove(“ entity1”);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM