package org.commcare.android.adapters;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;

import org.commcare.android.framework.ModifiableEntityDetailAdapter;
import org.commcare.android.models.Entity;
import org.commcare.android.view.EntityView;
import org.commcare.suite.model.Detail;
import org.javarosa.core.model.instance.TreeReference;

import java.util.List;
import java.util.Vector;

/**
 * Created by jschweers on 8/24/2015.
 * <p/>
 * Adapter for taking a nodeset, contextualizing it against an entity,
 * and then displaying one item for each node in the resulting set.
 */
public class EntitySubnodeDetailAdapter implements ListAdapter, ModifiableEntityDetailAdapter {

    private Context context;
    private Detail detail;
    private List<TreeReference> references;
    private List<Entity<TreeReference>> entities;
    private ListItemViewModifier modifier;

    public EntitySubnodeDetailAdapter(Context context, Detail detail, List<TreeReference> references, List<Entity<TreeReference>> entities, ListItemViewModifier modifier) {
        this.context = context;
        this.detail = detail;
        this.references = references;
        this.initializeData(references, entities);
    }

    /**
     * Populate entities and references. Will ignore any entities (and their
     * associated references) without any valid fields.
     */
    private void initializeData(List<TreeReference> references, List<Entity<TreeReference>> entities) {
        this.references = new Vector<>();
        this.entities = new Vector<>();
        int entityIndex = 0;
        for (Entity e : entities) {
            for (int i = 0; i < e.getNumFields(); i++) {
                if (e.isValidField(i)) {
                    this.entities.add(e);
                    this.references.add(references.get(entityIndex));
                    break;
                }
            }
            entityIndex++;
        }
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return false;
    }

    @Override
    public int getCount() {
        return references.size();
    }

    @Override
    public Object getItem(int position) {
        return references.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        EntityView view = (EntityView)convertView;
        Entity entity = this.entities.get(position);
        if (view == null) {
            view = new EntityView(context, detail, entity, null, null, position, false);
        } else {
            view.refreshViewsForNewEntity(entity, false, position);
        }
        if (modifier != null) {
            modifier.modify(view, position);
        }
        return view;
    }

    @Override
    public int getViewTypeCount() {
        return 1;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public boolean isEmpty() {
        return getCount() > 0;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
    }

    @Override
    public void setModifier(ListItemViewModifier modifier) {
        this.modifier = modifier;
    }
}

@@ -1271,31 +1271,27 @@
 his.
-references = references
+modifier = modifier
 ;%0D%0A 
@@ -1556,33 +1556,36 @@
 rence%3E reference
-s
+List
 , List%3CEntity%3CTr
@@ -1599,27 +1599,29 @@
 ence%3E%3E entit
-ies
+yList
 ) %7B%0D%0A       
@@ -1639,33 +1639,56 @@
 ences = new 
-Vector%3C%3E(
+ArrayList%3C%3E(referenceList.size()
 );%0D%0A        
@@ -1711,17 +1711,37 @@
 new 
-Vector%3C%3E(
+ArrayList%3C%3E(entityList.size()
 );%0D%0A
@@ -1799,19 +1799,21 @@
  : entit
-ies
+yList
 ) %7B%0D%0A   
@@ -1997,25 +1997,28 @@
 dd(reference
-s
+List
 .get(entityI

Update EntitySubnodeDetailAdapter.java