ArrayList只是识别第一个元素

时间:2018-07-25 03:49:58

标签: java android arrays arraylist static-variables

我有一个ArrayList,我在方法中添加了一些数据,当我尝试删除不是第一个项目时,出现以下错误

  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)

但是,如果我检查数组的大小,则表明数字增加了一个

我要删除的代码:

    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();
}

我正在使用以下代码向其中添加项目

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);

1 个答案:

答案 0 :(得分:2)

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.