与之相反的是:

savedPlanets.append(planet.getDisplayName()+",");

我有一个列表,每当用户单击一个复选框时,我都会添加行星的名称,如果清除了该复选框,我想从savedPlanets中删除该名称

最佳答案

您可以执行以下操作:

 savedPlanets.replace(planet.getDisplayName(),"");

理想情况下,我将执行以下解决方案:
Set<String> selectedPlanets = new HashSet<String>();

selectedPlanets.add(planet.getDisplayName());        // Whenever a planet is selected
selectedPlanets.remove(planet.getDisplayName());     // Whenever a planet is removed

// Prepare a String with all planets
StringBuilder savedPlanets = new StringBuilder("");
for(String planetName : selectedPlanets ){
    savedPlanets.append(planetName).append(",");
}
// Removing , from the end if any
if(savedPlanets.toString().endsWith(","))
    finalValue = savedPlanets.substring(0, savedPlanets.length()-1);

// finalValue is what you are looking for

关于java - 如何在Java中追加StringBuilder的附加内容?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/12353273/

10-10 11:13