com.adstrosoftware.gpsplayground.FeatureListFragment.java Source code

Java tutorial

Introduction

Here is the source code for com.adstrosoftware.gpsplayground.FeatureListFragment.java

Source

/**
 * Copyright 2013 Adstro Software
 *
 * 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 com.adstrosoftware.gpsplayground;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.View;
import android.widget.AbsListView;
import android.widget.ArrayAdapter;
import android.widget.ListView;

/**
 * Fragment to list the supported animations
 *
 * @author Adam Stroud &#60;<a href="mailto:adstro@adstrosoftware.com">adstro@adstrosoftware.com</a>&#62;
 */
public class FeatureListFragment extends ListFragment {

    private CallbackListener callbackListener;

    /**
     * No-arg constructor needed by Android
     */
    public FeatureListFragment() {
        // no-op
    }

    public static FeatureListFragment newInstance() {
        return new FeatureListFragment();
    }

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

        getListView().setChoiceMode(AbsListView.CHOICE_MODE_SINGLE);
        setListAdapter(new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_single_choice,
                getResources().getStringArray(R.array.featureArray)));
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        if (activity instanceof CallbackListener) {
            callbackListener = (CallbackListener) activity;
        } else {
            throw new InvalidFragmentActivityException(activity, CallbackListener.class);
        }
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        callbackListener.onFeatureSelected(position);
    }

    /**
     * Handles the callbacks for fragment actions
     */
    public interface CallbackListener {
        /**
         * Feature selection handler
         *
         * @param feature The feature index that was selected
         */
        public void onFeatureSelected(int feature);
    }
}