Android Open Source - android-facade-example Countries List Fragment






From Project

Back to project page android-facade-example.

License

The source code is released under:

MIT License

If you think the Android project android-facade-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.ruenzuo.weatherapp.fragments;
//  www . j  ava 2s. co m
import android.app.Activity;
import android.app.ListFragment;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.ruenzuo.weatherapp.R;
import com.ruenzuo.weatherapp.adapters.CountriesAdapter;
import com.ruenzuo.weatherapp.managers.WeatherAppManager;
import com.ruenzuo.weatherapp.models.Country;

import bolts.Continuation;
import bolts.Task;

/**
 * Created by ruenzuo on 07/05/14.
 */
public class CountriesListFragment extends ListFragment implements SwipeRefreshLayout.OnRefreshListener {

    private CountriesListFragmentListener listener;
    private SwipeRefreshLayout swipeContainer;

    public interface CountriesListFragmentListener {
        public void onCountrySelected(Country country);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof CountriesListFragmentListener) {
            listener = (CountriesListFragmentListener)activity;
        } else {
            throw new ClassCastException(activity.toString()
                    + " must implement CountriesListFragment.CountriesListFragmentListener");
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_countries, container, false);
        CountriesAdapter adapter = new CountriesAdapter(getActivity(), R.layout.row_country);
        setListAdapter(adapter);
        swipeContainer = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        swipeContainer.setOnRefreshListener(this);
        swipeContainer.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_blue_dark,
                android.R.color.holo_blue_bright,
                android.R.color.holo_blue_dark);
        swipeContainer.setRefreshing(true);
        refresh();
        return view;
    }

    @Override
    public void onRefresh() {
        refresh();
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);
        CountriesAdapter countriesAdapter = (CountriesAdapter) getListAdapter();
        listener.onCountrySelected(countriesAdapter.getItem(position));
    }

    private void refresh() {
        final long startTime = System.currentTimeMillis();
        WeatherAppManager.INSTANCE.getCountries().continueWith(new Continuation<Country[], Void>() {

            @Override
            public Void then(Task<Country[]> task) throws Exception {
                long stopTime = System.currentTimeMillis();
                long elapsedTime = stopTime - startTime;
                Log.i("WeatherApp", String.valueOf(elapsedTime));
                if (!task.isFaulted()) {
                    Country[] countries = task.getResult();
                    WeatherAppManager.INSTANCE.startSyncService(countries);
                    swipeContainer.setRefreshing(false);
                    CountriesAdapter countriesAdapter = (CountriesAdapter) getListAdapter();
                    countriesAdapter.clear();
                    countriesAdapter.addAll(countries);
                    countriesAdapter.notifyDataSetChanged();
                }
                return null;
            }

        }, Task.UI_THREAD_EXECUTOR);
    }

}




Java Source Code List

com.ruenzuo.weatherapp.activities.CitiesActivity.java
com.ruenzuo.weatherapp.activities.CountriesActivity.java
com.ruenzuo.weatherapp.activities.StationDataActivity.java
com.ruenzuo.weatherapp.activities.StationsActivity.java
com.ruenzuo.weatherapp.adapters.CitiesAdapter.java
com.ruenzuo.weatherapp.adapters.CountriesAdapter.java
com.ruenzuo.weatherapp.adapters.StationDataAdapter.java
com.ruenzuo.weatherapp.adapters.StationsAdapter.java
com.ruenzuo.weatherapp.application.WeatherAppApplication.java
com.ruenzuo.weatherapp.definitions.CitiesFetcher.java
com.ruenzuo.weatherapp.definitions.CitiesStorer.java
com.ruenzuo.weatherapp.definitions.CountriesFetcher.java
com.ruenzuo.weatherapp.definitions.CountriesStorer.java
com.ruenzuo.weatherapp.definitions.StationsFetcher.java
com.ruenzuo.weatherapp.definitions.StationsStorer.java
com.ruenzuo.weatherapp.extensions.NotFoundInDatabaseException.java
com.ruenzuo.weatherapp.extensions.NotFoundInMemoryException.java
com.ruenzuo.weatherapp.fragments.CitiesListFragment.java
com.ruenzuo.weatherapp.fragments.CountriesListFragment.java
com.ruenzuo.weatherapp.fragments.StationDataListFragment.java
com.ruenzuo.weatherapp.fragments.StationsListFragment.java
com.ruenzuo.weatherapp.helpers.DatabaseHelper.java
com.ruenzuo.weatherapp.helpers.MemoryCacheHelper.java
com.ruenzuo.weatherapp.helpers.NetworkingHelper.java
com.ruenzuo.weatherapp.helpers.TranslatorHelper.java
com.ruenzuo.weatherapp.managers.WeatherAppManager.java
com.ruenzuo.weatherapp.models.City.java
com.ruenzuo.weatherapp.models.Country.java
com.ruenzuo.weatherapp.models.StationData.java
com.ruenzuo.weatherapp.models.Station.java
com.ruenzuo.weatherapp.services.SyncService.java
com.ruenzuo.weatherapp.utils.WeatherAppUtils.java