首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何停止将项目添加到数组列表中?

如何停止将项目添加到数组列表中?
EN

Stack Overflow用户
提问于 2014-12-18 10:04:43
回答 4查看 1.5K关注 0票数 0

有两个标签,费用和收入。“费用”选项卡和“收入”选项卡包含不同的列表视图。当使用“费用”选项卡启动活动时,list显示数据库字符串数组项。如果列表中有3个项目,则在将选项卡切换到“收入”选项卡并返回到“费用”选项卡后,它将显示6个项目。再次将相同的值添加到列表中。怎么才能推翻这个?这是我的密码。

代码语言:javascript
复制
String[] theCategory;
String[] theMoneyType;
String[] theDescp;
String[] theDate;
String[] theAmount;

AllDataClass allData;

ListView exList;
private ArrayList<String> kId = new ArrayList<String>();           
private ArrayList<String> t_category = new ArrayList<String>();
private ArrayList<String> t_date = new ArrayList<String>();
private ArrayList<String> t_amount=new ArrayList<String>();
private ArrayList<String> t_moneytype=new ArrayList<String>();
private ArrayList<String> t_description=new ArrayList<String>();     <<< these array list values getting from SQlite database 

ArrayList<AllExpense> list=new ArrayList<AllExpense>();

theCategory=t_category.toArray(new String[t_category.size()]);

    theDate=t_date.toArray(new String[t_date.size()]);

    theAmount=t_amount.toArray(new String[t_amount.size()]);
    theMoneyType=t_moneytype.toArray(new String[t_moneytype.size()]);

    theDescp=t_description.toArray(new String[t_description.size()]);

    for(int i=0;i<theDate.length;i++) <<<<<< //this loop running again after switching the tab. I need this loop only one time.>>>>>>>>>>>
    {

        AllExpense ae=new AllExpense(theCategory[i],theDate[i],theAmount[i],theMoneyType[i],theDescp[i]);

        list.add(ae);

    }
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2014-12-18 10:12:48

在添加项目之前清除列表。

代码语言:javascript
复制
list.clear();

for(int i=0;i<theDate.length;i++) <<<<<< //this loop running again after switching the tab. I need this loop only one time.>>>>>>>>>>>
    {

        AllExpense ae=new AllExpense(theCategory[i],theDate[i],theAmount[i],theMoneyType[i],theDescp[i]);

        list.add(ae);

    }
票数 1
EN

Stack Overflow用户

发布于 2014-12-18 10:09:09

用于验证天气字符串是否可用的写入方法。

您还可以使用

代码语言:javascript
复制
list.clear();

前循环

票数 0
EN

Stack Overflow用户

发布于 2014-12-18 10:11:53

在添加之前检查:

代码语言:javascript
复制
private void test() {
    ArrayList<String> names=new ArrayList<String>();
    ArrayList<String> totalNames=db.getAllNames();
    for (int i = 0; i < totalNames.size(); i++) {
        if(!names.contains(totalNames.get(i))){
            names.add(totalNames.get(i));
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27543924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档