package com.handmark.pulltorefresh.sample;

import java.util.Arrays;
import java.util.LinkedList;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Gravity;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshGridView;

public class PullToRefreshGridActivity extends Activity {
	private LinkedList<String> mListItems;
	private PullToRefreshGridView mPullRefreshGridView;
	private GridView mGridView;
	private ArrayAdapter<String> mAdapter;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.pull_to_refresh_grid);

		mPullRefreshGridView = (PullToRefreshGridView) findViewById(R.id.pull_refresh_grid);
		mGridView = mPullRefreshGridView.getRefreshableView();

		// Set a listener to be invoked when the list should be refreshed.
		mPullRefreshGridView.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				// Do work to refresh the list here.
				new GetDataTask().execute();
			}
		});

		mListItems = new LinkedList<String>();
		
		TextView tv = new TextView(this);
		tv.setGravity(Gravity.CENTER);
		tv.setText("Empty View, Pull Down/Up to Add Items");
		mPullRefreshGridView.setEmptyView(tv);
		
		mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, mListItems);
		mGridView.setAdapter(mAdapter);
	}

	private class GetDataTask extends AsyncTask<Void, Void, String[]> {

		@Override
		protected String[] doInBackground(Void... params) {
			// Simulates a background job.
			try {
				Thread.sleep(2000);
			} catch (InterruptedException e) {
			}
			return mStrings;
		}

		@Override
		protected void onPostExecute(String[] result) {
			mListItems.addFirst("Added after refresh...");
			mListItems.addAll(Arrays.asList(result));
			mAdapter.notifyDataSetChanged();

			// Call onRefreshComplete when the list has been refreshed.
			mPullRefreshGridView.onRefreshComplete();

			super.onPostExecute(result);
		}
	}

	private String[] mStrings = { "Abbaye de Belloc", "Abbaye du Mont des Cats", "Abertam", "Abondance", "Ackawi",
	        "Acorn", "Adelost", "Affidelice au Chablis", "Afuega'l Pitu", "Airag", "Airedale", "Aisy Cendre",
	        "Allgauer Emmentaler" };
}

@@ -304,16 +304,45 @@
 extView;
+%0Aimport android.widget.Toast;
 %0A%0Aimport
@@ -412,16 +412,17 @@
 Listener
+2
 ;%0Aimport
@@ -1182,21 +1182,26 @@
 Listener
+2
 () %7B%0A
+%09%09%09%0A
 %09%09%09@Over
@@ -1222,16 +1222,26 @@
  void on
+PullDownTo
 Refresh(
@@ -1252,44 +1252,276 @@
 %09%09%09%09
-// Do work to refresh the list here.
+Toast.makeText(PullToRefreshGridActivity.this, %22Pull Down!%22, Toast.LENGTH_SHORT).show();%0A%09%09%09%09new GetDataTask().execute();%0A%09%09%09%7D%0A%0A%09%09%09@Override%0A%09%09%09public void onPullUpToRefresh() %7B%0A%09%09%09%09Toast.makeText(PullToRefreshGridActivity.this, %22Pull Up!%22, Toast.LENGTH_SHORT).show();
 %0A%09%09%09
@@ -1555,16 +1555,20 @@
 );%0A%09%09%09%7D%0A
+%09%09%09%0A
 %09%09%7D);%0A%0A%09
@@ -1607,18 +1607,16 @@
 ing%3E();%0A
-%09%09
 %0A%09%09TextV
@@ -1773,18 +1773,16 @@
 ew(tv);%0A
-%09%09
 %0A%09%09mAdap
@@ -2633,24 +2633,18 @@
 kawi%22,%0A%09
-        
+%09%09
 %22Acorn%22,
@@ -2738,16 +2738,10 @@
 %22,%0A%09
-        
+%09%09
 %22All

Change Sample Grid Activity to use new OnRefreshListener2