[英]Issues adding elements to an Arraylist. JSoup Android
2I need a little help with this code. 2我需要一些与此代码的帮助。 I'm getting an IndexOutOfBoundsException. 我收到了IndexOutOfBoundsException。 I know why but I'm stuck trying to fix it. 我知道为什么,但是我坚持尝试修复它。 Issue: eMain.getElementsByClass("trA1").size() = 8 and eMain.getElementsByClass("trA2").size() = 7 (The sizes many very, but trA1 is always bigger than trA2). 问题:eMain.getElementsByClass(“ trA1”)。size()= 8和eMain.getElementsByClass(“ trA2”)。size()= 7(大小很多,但是trA1总是大于trA2)。 I need to add each element to and ArrayList as add(trA1), add(trA2), add(trA1), add(trA2)...... (I'm using the for loop for that) but when I get to element 8, I get the exception because there is no trA2 element 8. I tried an If statement to check if the element is null or "" but I still get the exception. 我需要将每个元素添加到ArrayList中,作为add(trA1),add(trA2),add(trA1),add(trA2)……(我正在使用for循环),但是当我得到到元素8,我得到了异常,因为没有trA2元素8。我尝试了一个If语句来检查元素是否为null或“”,但是我仍然得到了异常。 any ideas. 有任何想法吗。
// 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())));
}
}
Since the size of "trA2" is always less than that of "trA1" you can do this as a fix. 由于“ trA2”的大小始终小于“ trA1”的大小,因此可以解决此问题。
Right before the for loop, declare: 在for循环之前,声明:
final int size2 = eMain.getElementsByClass("trA2").size();
In the for loop, change: 在for循环中,更改:
if (eMain.getElementsByClass("trA2").select("strong").get(i).text() != null)
to: 至:
if (i < size2 && eMain.getElementsByClass("trA2").select("strong").get(i).text() != null)
Offtopic but for the sake of code clarity, you might want to declare two variables element1
and element2
to eMain.getElementsByClass("trA1")
and eMain.getElementsByClass("trAeMain.getElementsByClass("trA2")
respectively right before the for loop as well, in order to make your code clearer. 脱离主题,但为了代码清晰起见,您可能还想在for循环之前分别向eMain.getElementsByClass("trA1")
和eMain.getElementsByClass("trAeMain.getElementsByClass("trA2")
声明两个变量element1
和element2
,以使您的代码更清晰。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.