net.d53.syman.mobile.ui.GaugeListFragment.java Source code

Java tutorial

Introduction

Here is the source code for net.d53.syman.mobile.ui.GaugeListFragment.java

Source

/*
 * Copyright 2012 GitHub Inc.
 *
 * 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.
 */

package net.d53.syman.mobile.ui;

import static net.d53.syman.mobile.IntentConstants.GAUGE;
import static net.d53.syman.mobile.IntentConstants.GAUGES;
import static net.d53.syman.mobile.IntentConstants.VIEW_AIR_TRAFFIC;
import static net.d53.syman.mobile.IntentConstants.VIEW_GAUGE;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.View;
import android.widget.ListView;

import com.actionbarsherlock.view.MenuItem;
import com.github.kevinsawicki.wishlist.SingleTypeAdapter;
import net.d53.syman.mobile.SymanServiceProvider;
import net.d53.syman.mobile.R.id;
import net.d53.syman.mobile.R.string;
import net.d53.syman.mobile.core.Gauge;
import com.google.inject.Inject;

import java.io.Serializable;
import java.util.List;

/**
 * Fragment to display a list of gauges
 */
public class GaugeListFragment extends ItemListFragment<Gauge> {

    @Inject
    private SymanServiceProvider serviceProvider;

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setEmptyText(string.no_gauges);
    }

    @Override
    protected void configureList(Activity activity, ListView listView) {
        super.configureList(activity, listView);

        listView.setFastScrollEnabled(true);
    }

    @Override
    public Loader<List<Gauge>> onCreateLoader(int id, Bundle args) {
        return new GaugeListLoader(getActivity(), items, serviceProvider);
    }

    @Override
    public void onListItemClick(ListView list, View view, int position, long id) {
        Gauge gauge = (Gauge) list.getItemAtPosition(position);
        Intent intent = new Intent(VIEW_GAUGE);
        intent.putExtra(GAUGE, gauge);
        startActivity(intent);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case id.settings:
            if (getActivity() != null) {
                Intent intent = new Intent(getActivity().getApplicationContext(), UserSettingsActivity.class);
                startActivity(intent);
            }
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected int getErrorMessage(Exception exception) {
        return string.error_loading_gauges;
    }

    @Override
    protected SingleTypeAdapter<Gauge> createAdapter(List<Gauge> items) {
        return new GaugeListAdapter(getActivity().getLayoutInflater(), items);
    }
}