Search code examples
javaandroidarraysarrayliststatic-variables

ArrayList just recognise the first element


I have an ArrayList that I add some data in a method, when I try to remove a item that isn't the first one I get the following error

  07-25 00:39:03.024 28960-28960/murilopereira.brofut E/[HAS CHECK]: 2
    07-25 00:39:03.025 28960-28960/murilopereira.brofut E/[CHECK INDEX]: udidjdid
    0
    84jdkd
    1
07-25 00:39:07.787 28960-29062/murilopereira.brofut D/OpenGLRenderer: endAllActiveAnimators on 0x7f6b86bc00 (MenuPopupWindow$MenuDropDownListView) with handle 0x7f6b8e6940
07-25 00:39:08.054 28960-28960/murilopereira.brofut E/[HAS CHECK]: 1
07-25 00:39:08.055 28960-28960/murilopereira.brofut E/[CHECK INDEX]: 84jdkd
    0
07-25 00:39:08.055 28960-28960/murilopereira.brofut D/AndroidRuntime: Shutting down VM
07-25 00:39:08.060 28960-28960/murilopereira.brofut E/AndroidRuntime: FATAL EXCEPTION: main
    Process: murilopereira.brofut, PID: 28960
    java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
        at java.util.ArrayList.get(ArrayList.java:437)
        at murilopereira.brofut.Times.deleteTeam(Times.java:203)
        at murilopereira.brofut.Times.access$100(Times.java:29)
        at murilopereira.brofut.Times$2.onClick(Times.java:109)
        at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
        at android.os.Handler.dispatchMessage(Handler.java:105)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6592)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:769)

But if I check the size of the array, that indicates a number uppon one

My code to delete:

    public static ArrayList<String> hasKeys = new ArrayList<>();
    ...
    private void deleteTeam(int indexRemove){
        Log.e("[HAS CHECK]", hasKeys.size() + "");
        for(int i = 0; i < hasKeys.size(); i++){
            Log.e("[CHECK INDEX]", hasKeys.get(i));
            Log.e("[CHECK INDEX]", i + "");
        }
        //        Log.e("[CHECK INDEX]", hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()) + "");
        hasKeys.remove(hasKeys.indexOf(m_orders.get(indexRemove).getTeamName()) - 1);
        Menu.teams.remove(m_orders.get(indexRemove).getTeamName());
        m_orders.remove(indexRemove);
        m_adapter.removeItem(indexRemove);
        m_adapter.notifyDataSetChanged();
}

I'm using the following code to add items to it

m_orders = new ArrayList<Teams>();
Teams o1 = new Teams();
hasKeys.add(teamName.getText().toString());
Log.e("[HAS CHECK]", hasKeys.size() + "");
o1.setTeamName(teamName.getText().toString());
o1.setTeamPlayers(playerOne.getText().toString() + ", " + playerTwo.getText().toString() + ", " + playerThree.getText().toString() + ", " + playerFour.getText().toString() + ", " + playerFive.getText().toString());
Menu.teams.put(o1.getTeamName(), o1.getTeamPlayers());
m_orders.add(o1);

Solution

  • This is happening because your item is always added at index 0. The size of array list is 1 every time from your add items. I don't know the exact flow of your code. But it's seems like if you are calling below statement every time:

    m_orders = new ArrayList<Teams>();
    

    This is instantiating your array list.