0

I'm trying to add multiple items to my arraylist by using this code:

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

        }

but it prompts up an error like this

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)

at line HashMapAdapter.kt:39 i have this code:

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

The problem is that I want to insert multiple items in my array.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • Why don't you use a `Map`? or `Map`? – OneCricketeer Jul 30 '17 at 06:28
  • Or define a simple class instead of trying to conform a HashMap – OneCricketeer Jul 30 '17 at 06:30
  • I'm trying to insert item into the ArrayList> , not printing it. –  Jul 30 '17 at 06:33
  • @cricket_007 java.lang.NumberFormatException: Invalid int: "[9, 11, 13, 14, 15, 17, 18]" –  Jul 30 '17 at 06:50
  • i tried making it String[] multiple hours, still the same errors @cricket_007 java.lang.NumberFormatException: Invalid int: "[9, 11, 13, 14, 15, 17, 18]" –  Jul 30 '17 at 06:54
  • Obviously an array is not an integer. Wherever you do `Integer.parseInt()` is not correct in `HashMapAdapter.getView` – OneCricketeer Jul 30 '17 at 09:01
  • @cricket_007 it needs to be integer, why you mark it as duplicate? it doesn't even answer my problem. –  Jul 30 '17 at 09:05
  • Because your error is clearly saying that you have incorrectly put an array to a string, which is what I thought you wanted to do based on `String.valueOf` – OneCricketeer Jul 30 '17 at 09:20

1 Answers1

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

You are getting an array as a String and trying to toInt() it?

That seems incorrect. You need to index the array(s) after parsing it back to a proper array object, but then you already have integers in them, so toInt() is not necessary.

If you really want the Map, you should use Map<String, Object[]> and lose the String.valueOf piece.


The output need to be like this:
Item 1: 9 - 45 - Departure - You need to go to destination 1
Item 2: 11 - 0 - Quezon Heritage House - Timeout, Go to next destination

If that is what you are trying to get, I suggest stop using a HashMap and arrays. You have a well-structured view and a POJO is what you need.

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

Obviously that is much simpler when using Kotlin data classes

Then, you don't need the HashMapAdapter, you can simply use 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);
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245