Android Open Source - android-facade-example Cities 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;
/*from   w  w w . j  a va2s.  com*/
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.CitiesAdapter;
import com.ruenzuo.weatherapp.managers.WeatherAppManager;
import com.ruenzuo.weatherapp.models.City;
import com.ruenzuo.weatherapp.models.Country;

import bolts.Continuation;
import bolts.Task;

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

    private Country country;
    private CitiesListFragmentListener listener;
    private SwipeRefreshLayout swipeContainer;

    public interface CitiesListFragmentListener {
        public void onCitySeleted(City city);
    }

    public Country getCountry() {
        return country;
    }

    public void setCountry(Country country) {
        this.country = country;
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_cities, container, false);
        CitiesAdapter adapter = new CitiesAdapter(getActivity(), R.layout.row_city);
        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);
        CitiesAdapter citiesAdapter = (CitiesAdapter) getListAdapter();
        listener.onCitySeleted(citiesAdapter.getItem(position));
    }

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

            @Override
            public Void then(Task<City[]> task) throws Exception {
                long stopTime = System.currentTimeMillis();
                long elapsedTime = stopTime - startTime;
                Log.i("WeatherApp", String.valueOf(elapsedTime));
                if (!task.isFaulted()) {
                    swipeContainer.setRefreshing(false);
                    CitiesAdapter citiesAdapter = (CitiesAdapter) getListAdapter();
                    citiesAdapter.clear();
                    citiesAdapter.addAll(task.getResult());
                    citiesAdapter.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