package be.ugent.zeus.resto.client.ui;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import be.ugent.zeus.resto.client.R;
import be.ugent.zeus.resto.client.data.Activity;
import be.ugent.zeus.resto.client.data.caches.ActivityCache;
import java.util.List;

/**
 *
 * @author Thomas Meire
 */
public class ActivityAdapter extends ArrayAdapter<Activity> {

  private List<Activity> activities;

  public ActivityAdapter(Context context, String date) {
    super(context, 0);
    activities = ActivityCache.getInstance(context).get(date);
  }

  @Override
  public int getCount () {
    return activities.size();
  }
  
  @Override
  public Activity getItem(int item) {
    return activities.get(item);
  }
  
  @Override
  public View getView(int item, View repurposed, ViewGroup parent) {
    View row = repurposed;
    if (row == null) {
      LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
      row = inflater.inflate(R.layout.activity_list_item, parent, false);
    }
    
    Activity activity = activities.get(item);
    
    String title = getContext().getResources().getString(R.string.activity_item_title);
    
    TextView titleView = (TextView) row.findViewById(R.id.activity_item_title);
    titleView.setText(String.format(title, activity.start, activity.title));
    
    String location = getContext().getResources().getString(R.string.activity_item_time_location);
    
    TextView locationView = (TextView) row.findViewById(R.id.activity_item_time_location);
    locationView.setText(String.format(location, activity.start, activity.end, activity.location));
    
    TextView association = (TextView) row.findViewById(R.id.activity_item_association);
    association.setText(activity.association_id);
    
    return row;
  }
}

@@ -65,16 +65,42 @@
 ontext;%0A
+import android.text.Html;%0A
 import a
@@ -342,69 +342,8 @@
 ty;%0A
-import be.ugent.zeus.resto.client.data.caches.ActivityCache;%0A
 impo
@@ -544,19 +544,33 @@
 xt, 
-String date
+List%3CActivity%3E activities
 ) %7B%0A
@@ -596,16 +596,21 @@
 0);%0A    
+this.
 activiti
@@ -618,52 +618,18 @@
 s = 
-A
+a
 ctivit
-yCache.getInstance(context).get(date)
+ies
 ;%0A  
@@ -672,32 +672,55 @@
  () %7B%0A    return
+ (activities != null) ?
  activities.size
@@ -721,16 +721,20 @@
 s.size()
+ : 0
 ;%0A  %7D%0A  
@@ -1391,32 +1391,46 @@
 tleView.setText(
+Html.fromHtml(
 String.format(ti
@@ -1466,16 +1466,17 @@
 .title))
+)
 ;%0A    %0A 
@@ -1694,16 +1694,30 @@
 setText(
+Html.fromHtml(
 String.f
@@ -1781,16 +1781,17 @@
 cation))
+)
 ;%0A    %0A 
@@ -1901,16 +1901,30 @@
 setText(
+Html.fromHtml(
 activity
@@ -1939,16 +1939,17 @@
 tion_id)
+)
 ;%0A    %0A 

Fix exception when no activities present & encoding issues