0

I am creating a calendar with events.
I used another library to create the calendar, but when I go to display the events, I can't get them to print in a list.
I tried using a TextView but it didn't work, so I tried with a ListView.

The ArrayAdapter can't call events as events is created in the for loop, yet when I put in the for loop it won't work.

cannot resolve constructor

I was wondering if is there something I am doing wrong?
Can I display it in a TextView instead?

Event ev1 = new Event(Color.GREEN, 1488052800000L, footballEv);

public void onDayClick(Date dateClicked) {

    List<Event> events = compactCalendarView.getEvents(dateClicked);

    ArrayAdapter<Event> adapter = new ArrayAdapter<Event>(this, android.R.layout.simple_list_item_1, event);


    ListView list = (ListView) findViewById(R.id.listView);
    list.setAdapter(adapter);

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
Sebmins
  • 13
  • 6

1 Answers1

0

cannot resolve constructor

You can check the API documentation for what constructors are available.

Look here...

 ArrayAdapter<String> adapter = new ArrayAdapter<String>(
        this,  // Context
        android.R.layout.simple_list_item_1,  // resource 
        event  // Error! Not a List<String>
);

If you want to show a list of Events, you need an ArrayAdapter<Event>.

And you should not need a loop to display them.

private ListView listView;
private ArrayAdapter<Event> adapter;
private List<Event> events;

public void onCreate(Bundle b) {
    ...

    listView = (ListView) findViewById(R.id.listView);
    events = new ArrayList<Event>();

    adapter = new ArrayAdapter<Event>(
        this, 
        android.R.layout.simple_list_item_1, 
        events);

    list.setAdapter(adapter);     
}

private void onDateClicked(Date clicked) {
    adapter.clear();
    adapter.addAll(compactCalendarView.getEvents(dateClicked));
}

Note: The Event class needs a toString() method.

Ref: How do I print my Java object without getting "SomeType@2f92e0f4"?

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • You are correct, I took it out of a loop and changed the `` to `` but I still have the same error. it is in a `public void onDayClick(Date dateClicked){ } ` method I don't know if that is why it isn't working? – Sebmins Feb 26 '17 at 19:30
  • Your question edit shows the use of an `event` variable that doesn't exist at all... – OneCricketeer Feb 26 '17 at 19:39
  • The `event` was in the for loop `for (Event event:events)` as you said I did not need to use a for loop I got rid of it. If I instead use `events` it still doesn't work the previous way it worked was with `event` and a for loop printing into a TextView but it wouldn't go onto separate lines so I changed it to a ListView. PS: I keep hitting enter for a new line and it sends the comment so I edit it, then you have already responded to it >: – Sebmins Feb 26 '17 at 19:50
  • Yes, it was, but your edit makes the question not compile also... I don't really know what error you get, but I updated my answer with how I would write the code – OneCricketeer Feb 26 '17 at 19:52
  • What does `adapter.addAll()` need? The code you wrote is so close to working but I need to put `event` in their for some reason, converting it to a String or Object just gives an error _cannot resolve method_ – Sebmins Feb 26 '17 at 20:17
  • `addAll()` takes a `Collection` where `T` is defined as the Object type that is defined from `ArrayAdapter`. In other words, `List` is an option, but not one `Event` – OneCricketeer Feb 26 '17 at 21:03