有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java过滤器ArrayList并删除不需要的元素

我有一个包含元素的ArrayList(字段是name和type)。我只希望在自己的列表视图中显示两种不同的可能类型(“edu”和“ent”)

我的想法是用相同的数据创建两个新的ArrayList,然后循环遍历每个ArrayList并过滤不需要的元素,如下所示:

ListView listView_ent = (ListView) findViewById(R.id.popular_apps_list_ent);
ArrayList<DataHolder> data_ent = data;
for (int i = 0; i < data_ent.size(); i++) {
    if(data_ent.get(i).getType().equals("edu")){
        data_ent.remove(i);
    }
}
listView_ent.setAdapter(new AppsAdapter(this, data_ent));

ListView listView_edu = (ListView) findViewById(R.id.popular_apps_list_edu);
ArrayList<DataHolder> data_edu = data;
for (int i = 0; i < data_edu.size(); i++) {
    if(data_edu.get(i).getType().equals("ent")){
        data_edu.remove(i);
    }
}
listView_edu.setAdapter(new AppsAdapter(this, data_edu));

ArrayList中有10个元素,每种类型5个

问题是,在两个列表视图的末尾,有4个相同的项目以混合类型显示

知道我做错了什么吗


共 (0) 个答案