com.handlerexploit.prime.example.activities.GridViewExample.java Source code

Java tutorial

Introduction

Here is the source code for com.handlerexploit.prime.example.activities.GridViewExample.java

Source

/*
 *  Copyright (c) 2012 Daniel Huckaby
 *
 *  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.handlerexploit.prime.example.activities;

import java.util.List;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.AsyncTaskLoader;
import android.support.v4.content.Loader;
import android.widget.AdapterView;
import android.widget.GridView;

import com.handlerexploit.prime.example.R;
import com.handlerexploit.prime.example.adapters.LazyImageAdapter;
import com.handlerexploit.prime.example.utils.Utilities;
import com.handlerexploit.prime.example.utils.Utilities.Image;
import com.handlerexploit.prime.example.utils.WrappedAsyncTaskLoader;

/**
 * This class implements and displays the {@link LazyImageAdapter} in the form
 * of a {@link GridView}. This example uses a {@link AsyncTaskLoader} internally
 * for generating our list of image urls.
 */
public class GridViewExample extends FragmentActivity implements LoaderCallbacks<List<Image>> {

    private LazyImageAdapter mLazyImageAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.grid_content);

        mLazyImageAdapter = new LazyImageAdapter(this, R.layout.grid_item);

        @SuppressWarnings("unchecked")
        AdapterView<LazyImageAdapter> adapterView = (AdapterView<LazyImageAdapter>) findViewById(android.R.id.list);
        adapterView.setEmptyView(findViewById(R.id.progressContainer));
        adapterView.setAdapter(mLazyImageAdapter);

        getSupportLoaderManager().initLoader(0, null, this);
    }

    @Override
    public Loader<List<Image>> onCreateLoader(int arg0, Bundle arg1) {
        return new WrappedAsyncTaskLoader<List<Image>>(this) {

            @Override
            public List<Image> loadInBackground() {
                return Utilities.getRandomThumbnailImages(GridViewExample.this);
            }
        };
    }

    @Override
    public void onLoadFinished(Loader<List<Image>> arg0, List<Image> arg1) {
        for (Image image : arg1) {
            mLazyImageAdapter.add(image);
        }
    }

    @Override
    public void onLoaderReset(Loader<List<Image>> arg0) {
        mLazyImageAdapter.clear();
    }
}