如何停止向arraylist添加项目?

时间:2014-12-18 10:04:43

标签: android listview items

有两个标签费用和收入。 “费用”选项卡和“收入”选项卡包含不同的列表视图使用“费用”选项卡启动活动时,列表显示数据库字符串数组项。如果列表中有3个项目,将选项卡切换到“收入”选项卡并返回“费用”选项卡后,将显示6个项目。再次将相同的值添加到列表中。如何覆盖这个?这是我的代码..

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);

    }

4 个答案:

答案 0 :(得分:1)

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

你也用

list.clear();

在for循环之前

答案 1 :(得分:0)

在添加之前检查:

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));
        }
    }
}

答案 2 :(得分:0)

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

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);

    }

答案 3 :(得分:0)

或另一种选择是

if(!list.IsEmpty()) {
    for(int i=0;i<theDate.length;i++)
    {
     ......
    }
}