2016-06-09 55 views
0

我剛剛收到java.lang.IndexOutOfBoundsException:但無法理解它是由什麼引起的。基本上,我正在將sqlite表的記錄整合到listview中。 這是我的sql查詢從表中獲取記錄。如何解決IndexOutOfBoundsException:索引1無效,大小爲1?

public ArrayList<Integer> getRecordsCount(int no) { 

    ArrayList<Integer> count = new ArrayList<>(); 

    Cursor c = database.rawQuery(
      "select count(*) as TotalCount, tDate, " + 
        "strftime('%d', tDate) as DayPart, " + 
        "strftime('%m', tDate) as MonthPart, " + 
        "strftime('%Y', tDate) as YearPart " + 
        "from library " + 
        "where type = '" + no + "' " + 
        "group by MonthPart, YearPart", null); 

    while (c.moveToNext()) { 
     count.add(c.getInt(0)); 
    } 

    return count; 
} 

這是我的方法檢索數據: -

public void setDataInList(int no) { 

    if (no == 0) { 

     ArrayList<Integer> count = helper.getRecordsCount(1); 

     mainGetLibraryModels.clear(); 
     MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no); 
     for (int i = 0; i < modelWS.getRecords().size(); i++) { 

      WSLibraryModel model = new WSLibraryModel(); 
      model.setAmount(modelWS.getRecords().get(i).getAmount()); 
      model.setTotalCount("" + count.get(i)); 
      model.setYearPart(modelWS.getRecords().get(i).getYearPart()); 
      model.setMonthPart(modelWS.getRecords().get(i).getMonthPart()); 

      mainGetLibraryModels.add(model); 

     } 
     adapter.notifyDataSetChanged(); 

    } else if (no == 1) { 

     ArrayList<Integer> count = helper.getRecordsCount(2); 

     mainGetLibraryModels.clear(); 
     MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no); 
     for (int i = 0; i < modelWS.getRecords().size(); i++) { 

      WSLibraryModel model = new WSLibraryModel(); 
      model.setAmount(modelWS.getRecords().get(i).getAmount()); 
      model.setTotalCount("" + count.get(i)); 
      model.setYearPart(modelWS.getRecords().get(i).getYearPart()); 
      model.setMonthPart(modelWS.getRecords().get(i).getMonthPart()); 

      mainGetLibraryModels.add(model); 
     } 
     adapter.notifyDataSetChanged(); 

    } else { 

     mainGetLibraryModels.clear(); 
     MainLibraryModelWS modelWS = helper.getAllRecordsMonthWise2(no); 
     for (int i = 0; i < modelWS.getRecords().size(); i++) { 

      WSLibraryModel model = new WSLibraryModel(); 
      model.setAmount(modelWS.getRecords().get(i).getAmount()); 
      model.setTotalCount(" - "); 
      model.setYearPart(modelWS.getRecords().get(i).getYearPart()); 
      model.setMonthPart(modelWS.getRecords().get(i).getMonthPart()); 

      mainGetLibraryModels.add(model); 

     } 
     adapter.notifyDataSetChanged(); 

    } 
} 

但是,當我運行這段代碼,它給了我這個錯誤?

FATAL EXCEPTION: main Process: com.teezom, PID: 14168 
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 
    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) 
    at java.util.ArrayList.get(ArrayList.java:308) 
+2

因爲count數組的大小爲1,所以你需要通過0索引來訪問 – ThiepLV

回答

1

首先是使用switch代替if-else分支,因爲我看到你的結果從查詢會給結果的修正數爲數組(開關更有效,提高可讀性)。

public void setDataInList(int no) { //just a suggestion not a answer 

    switch (no){ 
    case 0 : 
      //Your Code as you specified in your code context. 
      break; 
    case 1 : 
      //Your Code as you specified in your code context. 

      break; 
    case 2 : 
      //Your Code as you specified in your code context. 

     break; 
    default : 
     break; 
    } 

現在來到你的問題,如果你看到你的Exception堆棧跟蹤和調試應用程序,一旦你會很容易讓你的解決方案。

這是您的堆棧跟蹤says--

java.lang.IndexOutOfBoundsException: Invalid index 1, size is 1 

希望你知道這火爆異常IndexOutOfBoundsException只能排在你試圖訪問不存在於數組中的任何數組索引。

現在您的錯誤信息清楚地表明您的Array大小爲one。這意味着您的陣列只能通過索引zero(0)訪問。

所以,請調試代碼,並試圖找出其中的代碼行產生例外,事實上

2

取而代之的是:

ArrayList<Integer> count = helper.getRecordsCount(1); 

試試這個:

ArrayList<Integer> count = helper.getRecordsCount(0); 
+0

不,我不能。我得到的記錄使用否 – Mahesh

+0

@MaheshBpsk要在第一個位置訪問元素,您應該訪問元素爲0.索引從0開始。您正在檢查是否(不== 0)併發送1來訪問記錄。並在第二,而不是2把1 –

0

首先,你需要避免調用getRecords()那麼多次。它會降低性能,並且可能不會返回相同的結果 - 可能導致錯誤的原因。相反,引入一個本地參數作爲此函數的緩存輸出並與之一起玩。

+1

這應該是一個評論。你也沒有真正回答OP提出的問題。 – YoungHobbit

+2

我的聲望<50.我希望我能...... – Robo

相關問題