package com.kkbox.toolkit.example.listview;

import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.FrameLayout;

import com.kkbox.toolkit.api.KKAPIListener;
import com.kkbox.toolkit.example.ExampleActivity;
import com.kkbox.toolkit.example.R;
import com.kkbox.toolkit.example.SampleUtil;
import com.kkbox.toolkit.example.api.ExampleWeatherAPI;
import com.kkbox.toolkit.utils.KKDebug;
import com.kkbox.toolkit.widget.KKListView;

public class KKListViewActivity extends ExampleActivity {
	private static final String TAG = "KKListViewActivity";
	private KKListView mListView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		FrameLayout root = new FrameLayout(this);
		setContentView(root);
		mListView = new KKListView(this);
		mListView.setPullToRefresh(pullToRefreshListener);
		mListView.setLoadMore(loadMoreListener);
		root.addView(mListView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));

		String[] mTestItem = getResources().getStringArray(R.array.city);
		ArrayAdapter<String> mAdapter = new ArrayAdapter(KKListViewActivity.this, android.R.layout.simple_list_item_1, mTestItem);
		mListView.setAdapter(mAdapter);

	}

	private KKListView.OnRefreshListener pullToRefreshListener = new KKListView.OnRefreshListener() {
		@Override
		public void onRefresh() {
			KKDebug.i(TAG, "onRefresh");
			loadForecastData(false);
		}
	};

	private KKListView.OnLoadMoreListener loadMoreListener = new KKListView.OnLoadMoreListener() {
		@Override
		public void onLoadMore() {
			KKDebug.i(TAG, "onLoadMore");
			loadForecastData(true);
		}
	};

	private void loadForecastData(final boolean more) {
		ExampleWeatherAPI mAPI = new ExampleWeatherAPI();
		mAPI.setAPIListener(new KKAPIListener() {
			@Override
			public void onAPIComplete() {
				if (more) {
					mListView.loadMoreFinished();
				} else {
					mListView.loadCompleted();
				}
			}

			@Override
			public void onAPIError(int errorCode) {
				KKDebug.e("KKListViewActivity", "onAPIError");
			}
		});

		mAPI.start(SampleUtil.test_item[0]);
	}
}

@@ -628,16 +628,56 @@
 istView;
+%0A%09private ArrayAdapter%3CString%3E mAdapter;
 %0A%0A%09@Over
@@ -1173,37 +1173,16 @@
 ity);%0A%09%09
-ArrayAdapter%3CString%3E 
 mAdapter
@@ -1773,17 +1773,25 @@
 boolean 
-m
+usedLoadM
 ore) %7B%0A%09
@@ -1939,105 +1939,32 @@
 %09%09%09%09
-if (more) %7B%0A%09%09%09%09%09mListView.loadMoreFinished();%0A%09%09%09%09%7D else %7B%0A%09%09%09%09%09mListView.loadCompleted();%0A%09%09%09%09%7D
+refreshUI(usedLoadMore);
 %0A%09%09%09
@@ -2073,16 +2073,45 @@
 rror%22);%0A
+%09%09%09%09refreshUI(usedLoadMore);%0A
 %09%09%09%7D%0A%09%09%7D
@@ -2113,17 +2113,16 @@
 %7D%0A%09%09%7D);%0A
-%0A
 %09%09mAPI.s
@@ -2155,10 +2155,198 @@
 0%5D);%0A%09%7D%0A
+%09%0A%09private void refreshUI(boolean usedLoadMore) %7B%0A%09%09if (usedLoadMore) %7B%0A%09%09%09mListView.loadMoreFinished();%0A%09%09%7D else %7B%0A%09%09%09mListView.loadCompleted();%0A%09%09%7D%0A%09%09mListView.setAdapter(mAdapter);%09%0A%09%7D%0A
 %7D%0A

Fix loading completed haven't refresh UI issues