java.lang.IndexOutOfBoundsException:无效索引2,Arraylist中的大小是2?

时间:2014-10-28 08:58:40

标签: java android

我有一个听众,当我点击它将搜索arraylist的日期并获取我点击的日期然后当我更改月份时我将删除日期然后在我当前月份添加日期。

public void onSelectDate(Date date, View view) {
    JSONObject jsonObj = new JSONObject(jsonStr);
    JsonArray datesIn = jsonObj.getJSONArray(TAG_SCHEDULE);
    for (int i = 0; i < datesIn.length(); i++) {
        JSONObject c = datesIn.getJSONObject(i);
        String dateD = c.getString(TAG_DATED);
        String statusD = c.getString(TAG_STATUS);
        HashMap<String, String> contact = new HashMap<String, String>();
        contact.put("Tag_dated", dateD);
        contact.put("Tag_status", statusD);
        contactList.add(contact);
        Date s = formatter.parse(dateD);

        ArrayList<HashMap<String, String>> contactList= new ArrayList<HashMap<String,String>>();;
        if(formatter.format(date).equals(contactList.get(0).get(TAG_DATED))){
            if(contactList.get(0).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(0).get(TAG_STATUS).equals("Reserved"))
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
        }else if(formatter.format(date).equals(contactList.get(1).get(TAG_DATED))){
            if(contactList.get(1).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(1).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(2).get(TAG_DATED))){
            if(contactList.get(2).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(2).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(3).get(TAG_DATED))){
            if(contactList.get(3).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(3).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(4).get(TAG_DATED))){
            if(contactList.get(4).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(4).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else if(formatter.format(date).equals(contactList.get(5).get(TAG_DATED))){
            if(contactList.get(5).get(TAG_STATUS).equals("Occupied")){
                Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
            }else if(contactList.get(5).get(TAG_STATUS).equals("Reserved")){
                Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
            }
        }else{
            Toast.makeText(getApplicationContext(),"NP",Toast.LENGTH_SHORT).show();
        }
    }

我的问题是当我更改月份,然后点击它有错误的日期。

public void onChangeMonth(int month, int year) {
    contactList.clear();
    HashMap<String, String> contact = new HashMap<String, String>();
    contact.put("Tag_dated", dateD);
    contact.put("Tag_status", statusD);
    contactList.add(contact);
}

Logcat错误:

http://i.stack.imgur.com/Is35y.png

4 个答案:

答案 0 :(得分:0)

您正在创建名为ArrayList的新contactList,并为其添加一个元素;然后你试图从中检索几个元素!

答案 1 :(得分:0)

错误是海峡前进,大小是两个。索引从0开始,因此索引可用0和1而不是1和2。 你的循环可能需要大小 - 1。

而不是硬编码您的语句,例如: 如果(formatter.format(日期).equals(contactList.get(0)获得(TAG_DATED))){

尝试在你去的循环中进行

public void updateToast(Date date, ArrayList<HashMap<String, String>> contactList) {
    String formattedDate = formatter.format(date);
    for (int i = 0; i < contactList.size(); i++)
    {
        if (formattedDate.equals(contactList.get(i).get(TAG_DATED))) 
        {
           if(contactList.get(i).get(TAG_STATUS).equals("Occupied"))
           {
              Toast.makeText(getApplicationContext(),"S",Toast.LENGTH_SHORT).show();
              return;
           } else if(contactList.get(i).get(TAG_STATUS).equals("Reserved")) 
           {
              Toast.makeText(getApplicationContext(),"R",Toast.LENGTH_SHORT).show();
              return;
           }
        }
    }
    // If here then its not found in any lists throw up your other text.
    Toast.makeText(getApplicationContext(),"NP",Toast.LENGTH_SHORT).show();
}

其中一些可能有点过时,因为它在这里完全放手。

答案 2 :(得分:0)

在您的代码onSelectDate中,您说:

contactList.add(contact);

它只会将Hashmap添加到您的列表中一次,因此您可以在Arraylist上使用contactList.get(0)。但是你要说的是

contactList.get(1);//even two

因此它没有首先出现的元素,因此会抛出异常。

答案 3 :(得分:0)

让我们稍微重构你的代码,让我们摆脱这个丑陋的讨厌和不安全的if / else恐怖,并把它改成漂亮可爱的循环

    boolean found = false;
    String formattedDate = formatter.format(date);
    for (int i = 0; i < contactList.size(); i++) {
        if (formattedDate.equals(contactList.get(i).get(TAG_DATED))) {
            if (contactList.get(i).get(TAG_STATUS).equals("Occupied")) {
                Toast.makeText(getApplicationContext(), "S",
                        Toast.LENGTH_SHORT).show();
            } else if (contactList.get(1).get(TAG_STATUS)
                    .equals("Reserved")) {
                Toast.makeText(getApplicationContext(), "R",
                        Toast.LENGTH_SHORT).show();
            }
            found = true;
            break;
        }
    }
    if (!found) {
        Toast.makeText(getApplicationContext(), "NP",
                Toast.LENGTH_SHORT).show();
    }

此代码应与您的代码完全相同,但它不应该为您提供ArrayIndexOutOfBoundsException异常