将多个项添加到ArrayList <hashmap <string,string =“”>&gt;

时间:2017-07-30 06:26:23

标签: java android arraylist hashmap

我尝试使用以下代码向我的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);

        }

但它会提示像这样的错误

FATAL EXCEPTION: main
  Process: com.sumo.traffic, PID: 26101
  java.lang.NumberFormatException: Invalid int: "[I@429b9398"
      at java.lang.Integer.invalidInt(Integer.java:137)
      at java.lang.Integer.parse(Integer.java:374)
      at java.lang.Integer.parseInt(Integer.java:365)
      at java.lang.Integer.parseInt(Integer.java:331)
      at com.sumo.traffic.HashMapAdapter.getView(HashMapAdapter.kt:39)
      at android.widget.AbsListView.obtainView(AbsListView.java:2301)
      at android.widget.ListView.makeAndAddView(ListView.java:1811)
      at android.widget.ListView.fillDown(ListView.java:697)

在线HashMapAdapter.kt:39我有这段代码:

 val hourPicked = alarmList[ApplicationConstants.HOUR]!!.toInt();
 val minutesPicked = alarmList[ApplicationConstants.MINUTE]!!.toInt();

问题是我想在我的数组中插入多个项目。

1 个答案:

答案 0 :(得分:0)

val hourPicked = alarmList[ApplicationConstants.HOUR]!!.toInt();
val minutesPicked = alarmList[ApplicationConstants.MINUTE]!!.toInt();

您将数组作为字符串并尝试toInt()吗?

这似乎不正确。在将数组解析回正确的数组对象之后,您需要索引数组,但之后您已经有了整数,因此不需要toInt()

如果您真的想要地图,则应使用Map<String, Object[]>并丢失String.valueOf部分。

  

输出必须如下:
   项目1:9 - 45 - 出发 - 你需要去目的地1
   项目2:11 - 0 - 奎松遗产屋 - 超时,前往下一个目的地

如果这是你想要的,我建议停止使用HashMap和数组。您有一个结构良好的视图,POJO就是您所需要的。

public class AlarmClock {
    int hour, minute;
    String destination, reminder;

    public AlarmClock(int hour, int minute, String destination, String reminder) {
        this.hour = hour;
        this.minute = minute;
        // ... etc
    }

    @Override 
    public String toString() {
        return String.format("%d - %d - %s - %s", hour, minute, destination, reminder);
    }

    // getters and setters

显然,使用Kotlin数据类时要简单得多

然后,您不需要HashMapAdapter,只需使用ArrayAdapter

ArrayAdapter<AlarmClock> clockAdapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1);
clockAdapter.add(new AlarmClock(9, 45, "Departure", "You need to go to Destination 1"));
clockAdapter.add(new AlarmClock(11, 0, "Quezon Heritage House", "Timeout, go to next destination");
listView.setAdapter(clockAdapter);