heylee.android.weatherapp.MainActivity.java Source code

Java tutorial

Introduction

Here is the source code for heylee.android.weatherapp.MainActivity.java

Source

package heylee.android.weatherapp;

/* Copyright (C) 2015 Heylee authors
* 
* selfof0800@gmail.com
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
* http://www.apache.org/licenses/LICENSE-2.0
* 
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import heylee.android.model.Location;
import heylee.android.network.JSONParserCustom;
import heylee.android.network.WeatherClient;
import heylee.android.weatherapp.utils.PagerContainer;

import java.util.HashMap;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.widget.Toast;

/**
 * Simple Example..
 *  
 * @author junghyunlee
 *
 */
public class MainActivity extends FragmentActivity {

    static Context mContext;
    CPageAdapter pAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //TODO
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mContext = this;

        PagerContainer conteainer = (PagerContainer) findViewById(R.id.pager_container);
        ViewPager pager = conteainer.getViewPager();
        pager.setOffscreenPageLimit(6);
        pager.setPageMargin(40);
        pager.setClipChildren(false);

        pAdapter = new CPageAdapter(this.getSupportFragmentManager());
        pager.setAdapter(pAdapter);

        Location location = new Location();
        location.setLatitude(37.5658333333f);
        location.setLongitude(126.9788888889f);

        new JSONWeatherTask().execute(location);
    }

    /**
     * 
     * @author junghyunlee
     * 
     */
    private class JSONWeatherTask extends AsyncTask<Location, Void, Long> {
        private HashMap<String, Object> map;
        private HashMap<String, Object> detailMap;
        private Location locate;

        @Override
        protected Long doInBackground(Location... params) {
            try {
                locate = params[0];
                String simpleWeatherData = ((new WeatherClient()).getWeatherData(false,
                        String.valueOf(locate.getLatitude()), String.valueOf(locate.getLongitude())));
                map = JSONParserCustom.getJsonData(simpleWeatherData);
                String detailWeatherData = ((new WeatherClient()).getWeatherData(true,
                        String.valueOf(locate.getLatitude()), String.valueOf(locate.getLongitude())));
                detailMap = JSONParserCustom.getJsonData(detailWeatherData);
            } catch (Exception e) {
                e.printStackTrace();
                return 1L;
            }
            return 0L;
        }

        @Override
        protected void onPostExecute(Long result) {
            super.onPostExecute(result);
            if (result == 0L) {
                // ? .
                pAdapter.setData(map);
                pAdapter.setDetailData(detailMap);
                pAdapter.notifyDataSetChanged();
            } else {
                Toast.makeText(MainActivity.this, "weather Error!!", 3000).show();
            }
        }
    }
}