package org.pierrre.webimage.sample.activity;

import java.util.List;

import org.pierrre.adapteritem.AdapterItem;
import org.pierrre.webimage.sample.data.Country;
import org.pierrre.webimage.sample.item.CountryItem;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.actionbarsherlock.app.SherlockListFragment;

public class CountryListFragment extends SherlockListFragment implements AbsListView.RecyclerListener {
	private List<Country> countries;
	
	private CountryListAdapter adapter;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		this.countries = Country.getCountries(this.getResources());
		
		this.adapter = new CountryListAdapter();
		this.setListAdapter(this.adapter);
	}
	
	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);
		
		this.getListView().setRecyclerListener(this);
	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		
		Country country = this.countries.get(position);
		
		((CountryActivity) this.getActivity()).selectCountry(country);
	}
	
	@Override
	public void onMovedToScrapHeap(View view) {
		AdapterItem item = AdapterItem.getItem(view);
		
		if (item != null) {
			item.onViewMovedToScrapHeap();
		}
	}
	
	public void refresh() {
		this.adapter.notifyDataSetChanged();
	}
	
	private class CountryListAdapter extends BaseAdapter {
		@Override
		public int getCount() {
			return CountryListFragment.this.countries.size();
		}
		
		@Override
		public Object getItem(int position) {
			return CountryListFragment.this.countries.get(position);
		}
		
		@Override
		public long getItemId(int position) {
			return position;
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			CountryItem countryItem;
			
			if (convertView != null) {
				countryItem = AdapterItem.getItem(convertView);
			} else {
				countryItem = new CountryItem(CountryListFragment.this.getActivity());
				convertView = countryItem.getView();
			}
			
			countryItem.update(CountryListFragment.this.countries.get(position));
			
			return convertView;
		}
	}
}
@@ -298,43 +298,8 @@
 up;%0A
-import android.widget.AbsListView;%0A
 impo
@@ -329,16 +329,16 @@
 dapter;%0A
+
 import a
@@ -484,48 +484,8 @@
 ent 
-implements AbsListView.RecyclerListener 
 %7B%0A%09p
@@ -946,26 +946,19 @@
 %09%0A%09%09
-this.getListView()
+AdapterItem
 .set
@@ -974,16 +974,28 @@
 iste
-ne
 r(this
+.getListView()
 );%0A%09
@@ -1184,16 +1184,16 @@
 on);%0A%09%09%0A
+
 %09%09((Coun
@@ -1258,180 +1258,8 @@
 %7D%0A%09%0A
-%09@Override%0A%09public void onMovedToScrapHeap(View view) %7B%0A%09%09AdapterItem item = AdapterItem.getItem(view);%0A%09%09%0A%09%09if (item != null) %7B%0A%09%09%09item.onViewMovedToScrapHeap();%0A%09%09%7D%0A%09%7D%0A%09%0A
 %09pub

Change the recycler listener