com.sweetiepiggy.littlepro.BrowseSubjectsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.sweetiepiggy.littlepro.BrowseSubjectsActivity.java

Source

/*
Copyright (C) 2014-2015 Sweetie Piggy Apps <sweetiepiggyapps@gmail.com>
    
This file is part of Little Pro.
    
Little Pro is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
    
Little Pro is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with Little Pro; if not, see <http://www.gnu.org/licenses/>.
*/

package com.sweetiepiggy.littlepro;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.Toast;

public class BrowseSubjectsActivity extends LockedActionBarActivity
        implements DownloadSubjectListTask.OnDownloadedListener {
    private static final String FRAGMENT_TAG = "fragment_tag";

    private DownloadSubjectListTask mDownloadSubjectListTask = null;
    private BrowseSubjectsFragment mBrowseSubjectsFragment = null;
    private String mEmail = null;
    private long mCourseId = -1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_container);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        if (savedInstanceState == null) {
            mBrowseSubjectsFragment = new BrowseSubjectsFragment();
            getSupportFragmentManager().beginTransaction()
                    .add(android.R.id.content, mBrowseSubjectsFragment, FRAGMENT_TAG).commit();

            Bundle bundle = getIntent().getExtras();
            if (bundle != null) {
                loadState(bundle);
            }
        } else {
            mBrowseSubjectsFragment = (BrowseSubjectsFragment) getSupportFragmentManager()
                    .findFragmentByTag(FRAGMENT_TAG);
            loadState(savedInstanceState);
        }

        mDownloadSubjectListTask = new DownloadSubjectListTask(this, this, mCourseId);
        mDownloadSubjectListTask.execute();
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        savedInstanceState.putString("email", mEmail);
        savedInstanceState.putLong("courseId", mCourseId);
        super.onSaveInstanceState(savedInstanceState);
    }

    private void loadState(Bundle bundle) {
        mEmail = bundle.getString("email");
        mCourseId = bundle.getLong("courseId", -1);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        loadState(savedInstanceState);
    }

    @Override
    public void onDestroy() {
        if (mDownloadSubjectListTask != null) {
            mDownloadSubjectListTask.cancel(true);
        }
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_refresh, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.action_refresh:
            if (mDownloadSubjectListTask != null) {
                mDownloadSubjectListTask.cancel(true);
            }
            mDownloadSubjectListTask = new DownloadSubjectListTask(this, this, mCourseId);
            mDownloadSubjectListTask.execute();
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    public void onDownloaded(List<String> subjectNames, List<Long> subjectIds) {
        if (getApplicationContext() != null) {
            mBrowseSubjectsFragment.notifyDataSetChanged(subjectNames, subjectIds, mEmail, mCourseId);
        }
    }

    public static class BrowseSubjectsFragment extends ListFragment {
        private List<String> mSubjectNames = null;
        private List<Long> mSubjectIds = null;

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            if (container == null) {
                return null;
            }

            View view = inflater.inflate(R.layout.fragment_browse_subjects, container, false);
            return view;
        }

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

        public void notifyDataSetChanged(List<String> subjectNames, List<Long> subjectIds, final String email,
                final long courseId) {
            if (subjectNames != null && subjectIds != null) {
                mSubjectNames = subjectNames;
                mSubjectIds = subjectIds;
                ListAdapter adapter = new ArrayAdapter(getActivity().getApplicationContext(),
                        R.layout.simple_list_item_1, android.R.id.text1, mSubjectNames);
                setListAdapter(adapter);

                getListView().setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
                        long subjectId = mSubjectIds.get(pos);
                        Intent intent = new Intent(getActivity().getApplicationContext(),
                                BrowseQuizzesActivity.class);
                        Bundle bundle = new Bundle();
                        bundle.putString("email", email);
                        bundle.putLong("courseId", courseId);
                        bundle.putLong("subjectId", subjectId);
                        intent.putExtras(bundle);
                        startActivity(intent);
                    }
                });
            }
        }
    }
}