【问题标题】:Android inserting multiple single item into a ArrayList<HashMap<String, String>> from HashMap<String, String>Android 从 HashMap<String, String> 将多个单项插入 ArrayList<HashMap<String, String>>
【发布时间】:2018-01-05 23:27:00
【问题描述】:

我正在尝试将多个单个项目插入 ArrayList> 这是我的代码:

 public static ArrayList<HashMap<String, String>> alarmClocks = new ArrayList<>();
    public void q1() {
            int[] multipleHours = {9, 11, 13, 14, 15, 17, 18}; //store here the hours for every alarm you want to set
            int[] multipleMinutes = {45, 0, 0, 0, 45, 0, 45}; //store here the minutes
            String[] multipleDestinations = {"Departure", "Quezon Heritage House", "Art In Island", "Quezon City Experience", "Quezon Memorial", " Destination 5", "Destination 6"}; //same thing for destinations
            String[] multipleReminders = {"You need to go to Destination 1", "Timeout, Go to next destination", "Timeout, Go to next destination", "Timeout, Go to next destination", "Timeout, Go to next destination", "Timeout, Go to next destination", "Package Ended!"}; //and reminders
            HashMap<String, String> alarm = new HashMap<>();
            alarm.put(ApplicationConstants.HOUR, String.valueOf(multipleHours));
            alarm.put(ApplicationConstants.MINUTE, String.valueOf(multipleMinutes));
            alarm.put(ApplicationConstants.REMINDER, String.valueOf(multipleReminders));
            alarm.put(ApplicationConstants.DESTINATION, String.valueOf(multipleDestinations));
    alarmClocks.add(alarm);

        }

插入应该是这样的:

项目 1:9 - 45 - 出发 - 你需要去目的地 1

项目 2:11 - 0 - Quezon Heritage House - 超时,前往下一个目的地。

我的问题是代码包含数组中的所有项目

错误提示如下:

java.lang.NumberFormatException: Invalid int: "[9, 11, 13, 14, 15, 17, 18]"

【问题讨论】:

    标签: android string arraylist hashmap


    【解决方案1】:

    尝试使用for循环:

    for (int i = 0; i<multipleHours.length;i++){
    HashMap<String, String> alarm = new HashMap<>();
            alarm.put(ApplicationConstants.HOUR, String.valueOf(multipleHours[i]));
            alarm.put(ApplicationConstants.MINUTE, String.valueOf(multipleMinutes[i]));
            alarm.put(ApplicationConstants.REMINDER, String.valueOf(multipleReminders[i]));
            alarm.put(ApplicationConstants.DESTINATION, String.valueOf(multipleDestinations[i]));
    alarmClocks.add(alarm);
    }
    

    但请记住所有数组长度应该相同,否则会出现 indexoutofbound 错误。

    【讨论】:

    • 我会回答的(迟到了),但是这个兄弟是正确使用循环
    • 我很抱歉缺少有关问题的详细信息,但我需要将代码放入 onClick。
    • 您希望在哪个视图上点击?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-27
    • 1970-01-01
    • 2013-07-29
    • 1970-01-01
    • 1970-01-01
    • 2013-05-08
    相关资源
    最近更新 更多