【问题标题】:how to clear the ArrayList after adding that ArrayList to HashMap in Android?将 ArrayList 添加到 Android 中的 HashMap 后如何清除 ArrayList?
【发布时间】:2014-10-17 05:40:07
【问题描述】:

我正在做一个小的安卓应用。我有 Expandable listview 并试图从 SQLite 填充数据。 所以我使用 HashMap 来存储 parent(String)child(ArrayList) .

例如,我在父级(1、2、3、4)中有四个整数编号,我得到这个并存储在 ListArray 中。现在我需要为那个特定的父母获取 child(ListArray) 。父 1 包含 (aa, bb),父 2 包含 (cc).. 等等,现在看这段代码

try 
    {
        header = new ArrayList<String>();
        footer = new ArrayList<String>();
        child= new HashMap<String, List<String>>();
        String date = null;

        int count=0, countchild=0;
        Cursor c = db.rawQuery("SELECT Date FROM Table", null);
        if (c.moveToFirst()) 
        {
            do 
            {
                date=c.getString(c.getColumnIndex("Date"));
                header.add(date);
                count=count+1;

            } while (c.moveToNext());
        }

        for (int i = 0; i < count; i++) 
        {
            String temp=header.get(i);
            Cursor cc=db.rawQuery("SELECT Id FROM Table WHERE Date ='"+ temp +"' ", null);
            if(cc.moveToFirst())
            {
                do 
                {
                    countchild=countchild+1;
                    String id=cc.getString(cc.getColumnIndex("Id"));
                    //footer.clear();   // it clear all data's on child
                    footer.add(id);
                    Log.d("Footer date count", "" + countchild);
                    child.put(temp, footer);
                } 
                while (cc.moveToNext());

                //footer.clear();  // it clear all data's on child
            }
            footer.clear();     // it clear all data's on child
        }

    } 

我想在将 footer 添加到子项后清除此处的footer,然后我才能获得正确的可扩展列表视图。如果我删除/评论此footer.clear() 行,则所有孩子都将添加到所有父母。 例如,父 1 包含(aa、bb、cc 等),父 2 包含(aa、bb、cc 等)。如果我离开这个footer.clear() 行,那么所有孩子都会被清除并只显示这样的父母,例如父母1(),父母2()......等

footer(ListArray)添加到HashMap后如何清除它??或告诉我一些修改此代码的建议。

谢谢。斯里哈里

【问题讨论】:

    标签: android sqlite arraylist hashmap expandablelistview


    【解决方案1】:

    当您将 put ArrayList 转换为 HashMap 时,HashMap 实际上保存了对您的数组的引用,而不是副本。

    这就是为什么您必须为每个组使用不同的数组:

    for (int i = 0; i < count; i++)
    {
        List<String> children = new ArrayList<String>();
        String temp = header.get(i);
        Cursor cc = db.rawQuery("SELECT Id FROM Table WHERE Date ='"+ temp +"' ", null);
        if(cc.moveToFirst())
        {
            countChild += cc.getCount();
            Log.d("Footer date count", "" + countchild);
            do
            {
                String id = cc.getString(cc.getColumnIndex("Id"));
                children.add(id);
                child.put(temp, children);
            }
            while (cc.moveToNext());
        }
    }
    

    正如您所见,这里为每个组创建了一个新的childrenArrayList,它永远不会cleared,因为这也会清除HashMap 中的值。

    此外,我让自己修复了一些其他问题,例如使用CursorgetCount 方法来获取计数而不是循环。此外,我没有使用 footer 数组,因为它在您显示的代码中是不必要的。

    最后,请为您的变量使用易于理解且有意义的名称,因为它可以帮助您以及阅读您的代码的其他人。

    【讨论】:

    • 嗨@user3249477 非常感谢,现在我理解并感谢您的建议,此后我使用变量的确切名称。 :)
    【解决方案2】:

    记住 Java 是面向对象的,所以如果你清除一个对象的实例,在本例中是一个 ArrayList,引用它的所有其他对象都将获得该新值。

    我建议您在每次需要填充可扩展列表视图的新组时创建一个新的页脚变量实例,这样您创建的每个页脚列表实例都将与列表中的一个组“关联”。

    希望这会有所帮助。 :)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-02-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-11-08
      • 2015-03-30
      • 1970-01-01
      相关资源
      最近更新 更多