Android Open Source - AquaBase Species List Fragment






From Project

Back to project page AquaBase.

License

The source code is released under:

GNU General Public License

If you think the Android project AquaBase 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

/*
 *  This file is part of AquaBase.//from   w w  w  .ja va2 s  .  c om
 *
 *  AquaBase 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.
 *
 *  AquaBase 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 AquaBase.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Copyright (c) 2014 Cdric Bosdonnat <cedric@bosdonnat.fr>
 */
package org.aquabase;

import org.aquabase.data.AquabaseContentProvider;
import org.aquabase.data.CrustaceanTableHelper;
import org.aquabase.data.DatabaseHelper;
import org.aquabase.data.FishTableHelper;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

public class SpeciesListFragment extends ListFragment
        implements LoaderCallbacks<Cursor> {
    /**
     * The fragment argument representing the section number for this
     * fragment.
     */
    private static final String ARG_SECTION_NUMBER = "section_number"; //$NON-NLS-1$

    private String mTableName = null;
    private SimpleCursorAdapter mAdapter = null;


    /**
     * Returns a new instance of this fragment for the given section number.
     */
    public static SpeciesListFragment newInstance(int sectionNumber) {
        SpeciesListFragment fragment = new SpeciesListFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);

        // Populate the list view
        String[] columns = new String[]{DatabaseHelper.COLUMN_SCI_NAME};
        int[] fields = new int[]{android.R.id.text1};

        switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
            case 1:
                mTableName = FishTableHelper.TABLE_NAME;

                break;
            case 2:
                mTableName = CrustaceanTableHelper.TABLE_NAME;
                break;
        }

        if (mTableName != null) {
            getLoaderManager().initLoader(0, null, this);

            mAdapter = new SimpleCursorAdapter(getActivity(),
                    android.R.layout.two_line_list_item,
                    null, columns, fields, 0);
            setListAdapter(mAdapter);
        }

        return rootView;
    }

    @Override
    public void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = new Intent(getActivity(), DetailsActivity.class);
        Uri uri = Uri.parse(AquabaseContentProvider.BASE_URI + mTableName + "/" + id); //$NON-NLS-1$
        intent.putExtra(DetailsActivity.URI, uri);
        startActivity(intent);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        ((MainActivity) activity).onSectionAttached(getArguments().getInt(
                ARG_SECTION_NUMBER));
    }

    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        String uriStr = AquabaseContentProvider.BASE_URI + mTableName;
        String[] projection = new String[]{DatabaseHelper.COLUMN_ID,
                                           DatabaseHelper.COLUMN_SCI_NAME};
        CursorLoader cursorLoader = new CursorLoader(getActivity(),
            Uri.parse(uriStr), projection, null, null, null);
        return cursorLoader;
    }

    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        mAdapter.swapCursor(data);
    }

    public void onLoaderReset(Loader<Cursor> data) {
        mAdapter.swapCursor(null);
    }
}




Java Source Code List

org.aquabase.DetailsActivity.java
org.aquabase.DetailsFragment.java
org.aquabase.MainActivity.java
org.aquabase.NavigationDrawerFragment.java
org.aquabase.SpeciesListFragment.java
org.aquabase.data.AquabaseContentProvider.java
org.aquabase.data.CSVParser.java
org.aquabase.data.CrustaceanTableHelper.java
org.aquabase.data.CsvMapping.java
org.aquabase.data.DatabaseHelper.java
org.aquabase.data.FishTableHelper.java
org.aquabase.data.TableHelper.java