package com.example.adapters;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.R;
import com.example.models.Listing;
import com.example.singletons.Listings;

/**
 * Created by WX009-PC on 2/19/14.
 */
public class ListingAdapter extends BaseAdapter {

    private LayoutInflater mLayoutInflater;

    public ListingAdapter(LayoutInflater layoutInflater) {
        mLayoutInflater = layoutInflater;
    }

    @Override
    public int getCount() {
        return Listings.getInstance().getListings().size();
    }

    @Override
    public Object getItem(int i) {
        return Listings.getInstance().getListings().get(i);
    }

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

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        if(view == null) {
            viewHolder = new ViewHolder();
            view = mLayoutInflater.inflate(R.layout.row_listview, null);
            viewHolder.tvTitle = (TextView) view.findViewById(R.id.listing_title);
            viewHolder.tvDesc = (TextView) view.findViewById(R.id.listing_address);
            viewHolder.tvDate = (TextView) view.findViewById(R.id.listing_province);
            view.setTag(viewHolder);
        }
        else{
            viewHolder = (ViewHolder) view.getTag();
        }

        Listing listing = Listings.getInstance().getListings().get(i);
        viewHolder.tvTitle.setText(listing.getName());
        viewHolder.tvDesc.setText(listing.getAddress().getStreet());
        viewHolder.tvDate.setText(listing.getAddress().getCity());
        return view;
    }

    private class ViewHolder {
        TextView tvTitle;
        TextView tvDesc;
        TextView tvDate;
    }
}

@@ -1101,26 +1101,31 @@
  viewHolder.
-tv
+listing
 Title = (Tex
@@ -1193,22 +1193,30 @@
 wHolder.
-tvDesc
+listingAddress
  = (Text
@@ -1285,21 +1285,30 @@
 wHolder.
-tvDat
+listingProvinc
 e = (Tex
@@ -1571,18 +1571,23 @@
 wHolder.
-tv
+listing
 Title.se
@@ -1631,22 +1631,30 @@
 wHolder.
-tvDesc
+listingAddress
 .setText
@@ -1708,21 +1708,30 @@
 wHolder.
-tvDat
+listingProvinc
 e.setTex
@@ -1841,18 +1841,23 @@
 extView 
-tv
+listing
 Title;%0A 
@@ -1876,14 +1876,22 @@
 iew 
-tvDesc
+listingAddress
 ;%0A  
@@ -1909,13 +1909,22 @@
 iew 
-tvDat
+listingProvinc
 e;%0A 

rename ViewHolder fields to reflect listings