有两个标签,费用和收入。“费用”选项卡和“收入”选项卡包含不同的列表视图。当使用“费用”选项卡启动活动时,list显示数据库字符串数组项。如果列表中有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);
}发布于 2014-12-18 10:12:48
在添加项目之前清除列表。
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);
}发布于 2014-12-18 10:09:09
用于验证天气字符串是否可用的写入方法。
您还可以使用
list.clear();前循环
发布于 2014-12-18 10:11:53
在添加之前检查:
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));
}
}
}https://stackoverflow.com/questions/27543924
复制相似问题