一旦用户单击该特定项目,我就尝试删除该列表项目。
我创建了一个函数来进行样式更改
const completed = () =>{
return document.getElementById("demo").style.textDecoration='line-through'
};
Run Code Online (Sandbox Code Playgroud)
列表生成如下,我使用了material ui库
<List dense={dense} >
{items.slice(0).reverse().map(x=> (
<ListItem key={x.id} button id="demo" onClick={completed} divider>
<ListItemText primary={listItems(x)}/>
<ListItemSecondaryAction />
</ListItem>
))}
</List>
Run Code Online (Sandbox Code Playgroud)
从我编写的代码中,我只能删除列表的第一项。每当我添加新项目时,我唯一能够删除的项目总是第一个项目。
我正在尝试找到一种方法将其应用于列表中的所有元素
我正在尝试遍历一个 ArrayList,并将每个索引值与默认值进行比较,如果索引值与默认值匹配,我想返回 true,唯一的问题是,它总是只对索引项返回 true被添加。由于我的类没有 main 方法,因此我在类构造函数初始化期间添加了这些值。
public class CountryFinderImpl implements CountryFinder{
List<String> Countries = new ArrayList<String>();
public CountryFinderImpl() {
Countries.add("canada");
Countries.add("japan");
Countries.add("usa");
}
@Override
public boolean forWeather(String country) {
// TODO Auto-generated method stub
country = country.toLowerCase();
boolean c=false;
for(int i=0; i<Countries.size();i++) {
if(Countries.get(i).equals(country)) {
//System.out.println(country+"Weather available");
c=true;
}else {
//System.out.println(country+"weather unavilable");
c=false;
}
}
return c;
}
}
Run Code Online (Sandbox Code Playgroud)
country 参数是从另一个类传递的,该类从用户那里获取国家/地区值。