向Arraylist添加元素的问题。 JSoup Android

时间:2013-06-08 00:07:55

标签: java android arraylist jsoup indexoutofboundsexception

2我需要一些关于此代码的帮助。我得到一个IndexOutOfBoundsException。我知道为什么,但我一直试图解决它。问题:eMain.getElementsByClass(“trA1”)。size()= 8和eMain.getElementsByClass(“trA2”)。size()= 7(大小非常多,但trA1总是大于trA2)。我需要添加每个元素和ArrayList作为add(trA1),添加(trA2),添加(trA1),添加(trA2)......(我正在使用for循环)但是当我得到到元素8,我得到异常,因为没有trA2元素8.我尝试了一个If语句来检查元素是否为null或“”但我仍然得到异常。任何想法。

                // game list data
            if (!eMain.equals("")) {
                for (int i=0; i < eMain.getElementsByClass("trA1").size(); i++) {
                    // check if AsyncTask was cancelled
                    if (isCancelled())
                        break;

                    if (eMain.getElementsByClass("trA1").select("strong").get(i).text() != null) {
                        // get string data
                        String titleA1 = eMain.getElementsByClass("trA1").select("strong").get(i).text();
                        String achAmountA1 =  eMain.getElementsByClass("trA1").select("td[align]").get(mAchAmountCounter).text();
                        String gsAmountA1 = eMain.getElementsByClass("trA1").select("td[align]").get(mGsAmountCounter).text();
                        String aPageLinkA1 = eMain.getElementsByClass("trA1").select("td a").get(mPageLinkCounter).attr("abs:href");
                        String iconSrcA1 = eMain.getElementsByClass("trA1").select("td a img").get(i).attr("abs:src");

                        // create game object
                        GameObject gameObjectA1 = new GameObject();
                        gameObjectA1.setGlTitle(titleA1);
                        gameObjectA1.setGlAchAmount(achAmountA1 + " achievements");
                        gameObjectA1.setGlGSAmount(gsAmountA1 + " gamer score");
                        gameObjectA1.setGlAchPageUrl(aPageLinkA1);
                        gameObjectA1.setGlIcon(iconSrcA1);
                        mGameObjectList.add(gameObjectA1);
                    }

                    if (eMain.getElementsByClass("trA2").select("strong").get(i).text() != null) {
                        String titleA2 = eMain.getElementsByClass("trA2").select("strong").get(i).text();
                        String achAmountA2 =  eMain.getElementsByClass("trA2").select("td[align]").get(mAchAmountCounter).text();
                        String gsAmountA2 = eMain.getElementsByClass("trA2").select("td[align]").get(mGsAmountCounter).text();
                        String aPageLinkA2 = eMain.getElementsByClass("trA2").select("td a").get(mPageLinkCounter).attr("abs:href");
                        String iconSrcA2 = eMain.getElementsByClass("trA2").select("td a img").get(i).attr("abs:src");

                        GameObject gameObjectA2 = new GameObject();
                        gameObjectA2.setGlTitle(titleA2);
                        gameObjectA2.setGlAchAmount(achAmountA2 + " achievements");
                        gameObjectA2.setGlGSAmount(gsAmountA2 + " gamer score");
                        gameObjectA2.setGlAchPageUrl(aPageLinkA2);
                        gameObjectA2.setGlIcon(iconSrcA2);
                        mGameObjectList.add(gameObjectA2);
                    }

                    // counters
                    mPageLinkCounter += 3;
                    mAchAmountCounter += 2;
                    mGsAmountCounter += 2;

                    // update progress bar
                    publishProgress((100 / (Integer) Math.round(eA1.size())));
                }
            }

1 个答案:

答案 0 :(得分:1)

由于“trA2”的大小始终小于“trA1”的大小,因此您可以将其作为修复。

在for循环之前,声明:

final int size2 = eMain.getElementsByClass("trA2").size();

在for循环中,更改:

if (eMain.getElementsByClass("trA2").select("strong").get(i).text() != null)

为:

if (i < size2 && eMain.getElementsByClass("trA2").select("strong").get(i).text() != null)

Offtopic但为了清晰代码,您可能希望在for循环之前分别声明两个变量element1element2eMain.getElementsByClass("trA1")eMain.getElementsByClass("trAeMain.getElementsByClass("trA2") ,以使您的代码更清晰。