fr.eoit.activity.ItemListActivity.java Source code

Java tutorial

Introduction

Here is the source code for fr.eoit.activity.ItemListActivity.java

Source

/**
 * Copyright (C) 2012 Picon software
 *
 * This program 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 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
package fr.eoit.activity;

import android.app.SearchManager;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;
import fr.eoit.EOITConst;
import fr.eoit.R;
import fr.eoit.activity.fragment.util.MissingAPITips;
import fr.eoit.activity.listener.ItemOnItemListClickListener;
import fr.eoit.activity.util.AmazingListView;
import fr.eoit.activity.util.AmazingSimpleCursorAdapter;
import fr.eoit.activity.util.AmazingSimpleCursorAdapter.OrderType;
import fr.eoit.activity.util.ItemListViewBinder;
import fr.eoit.activity.util.compat.ActionBarActivity;
import fr.eoit.db.bean.Item;
import fr.eoit.db.bean.Prices;
import fr.eoit.db.dto.ColumnsNames;
import fr.eoit.parameter.Parameters;
import fr.piconsoft.activity.fragment.util.SimpleOkDialog;
import fr.piconsoft.db.util.DbUtil;

public class ItemListActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> {

    private static final String[] dataColumns = { Item._ID, Item.COLUMN_NAME_NAME };
    private static final String[] dataColumnsSearch = { Item._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 };
    private static final int[] viewIDs = { R.id.ITEM_ICON, R.id.ITEM_NAME };

    protected final static int FAVORITE_LOADER_ID = EOITConst.getNextLoaderIdSequence();
    protected final static int SEARCH_LOADER_ID = EOITConst.getNextLoaderIdSequence();

    private OrderType orderType = OrderType.CATEGORY;
    private String query;
    // private SearchView searchView;
    private AmazingSimpleCursorAdapter adapter;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.itemlist);

        Intent intent = getIntent();
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
            // handles a search query
            query = intent.getStringExtra(SearchManager.QUERY);
            showResults();
        } else if (Intent.ACTION_VIEW.equals(intent.getAction())) {
            // handles a click on a search suggestion; launches activity to show word
            Intent itemIntent = new Intent(this, ItemInfo2Activity.class);
            itemIntent.setData(intent.getData());
            startActivity(itemIntent);
            finish();
        } else {
            // If there is no data associated with the Intent, sets the data to the default URI, which
            // accesses a list of notes.
            if (intent.getData() == null) {
                intent.setData(Item.CONTENT_URI);
            }

            // Creates the backing adapter for the ListView.
            adapter = new AmazingSimpleCursorAdapter(this, R.layout.row, dataColumns, viewIDs);

            adapter.setViewBinder(new ItemListViewBinder());

            AmazingListView itemListView = (AmazingListView) findViewById(R.id.ITEM_LIST);
            itemListView.setOnItemClickListener(new ItemOnItemListClickListener());
            itemListView.setPinnedHeaderView(
                    LayoutInflater.from(this).inflate(R.layout.row_header, itemListView, false));
            // Sets the ListView's adapter to be the cursor adapter that was just created.
            itemListView.setAdapter(adapter);

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

        MissingAPITips dialog = new MissingAPITips(this);
        if (dialog.isActive(this) && Parameters.characterId == -1 && Parameters.keyId == -1) {
            dialog.show(getSupportFragmentManager(), "missing_api_dialog");
        }
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        if (id == FAVORITE_LOADER_ID) {
            return new CursorLoader(this, getIntent().getData(),
                    new String[] { Item._ID, Item.COLUMN_NAME_NAME, Item.COLUMN_NAME_VOLUME,
                            Item.COLUMN_NAME_CHOSEN_PRICE_ID, Prices.COLUMN_NAME_BUY_PRICE,
                            Prices.COLUMN_NAME_SELL_PRICE, Prices.COLUMN_NAME_PRODUCE_PRICE,
                            Prices.COLUMN_NAME_OWN_PRICE, ColumnsNames.Categories.COLUMN_NAME_NAME_ALIAS },
                    Item.COLUMN_NAME_FAVORITE + " = 1", null,
                    AmazingSimpleCursorAdapter.getOrderByQuery(orderType));
        } else if (id == SEARCH_LOADER_ID) {
            return new CursorLoader(this, Item.CONTENT_URI_SEARCH,
                    new String[] { Item._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 }, null, new String[] { query },
                    SearchManager.SUGGEST_COLUMN_TEXT_1);
        } else {
            return null;
        }
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        if (DbUtil.hasAtLeastOneRow(data)) {
            adapter.setOrderType(orderType);
            adapter.swapCursor(data);
            findViewById(android.R.id.empty).setVisibility(View.GONE);
            findViewById(R.id.ITEM_LIST).setVisibility(View.VISIBLE);
        } else {
            findViewById(R.id.ITEM_LIST).setVisibility(View.GONE);
            findViewById(android.R.id.empty).setVisibility(View.VISIBLE);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        adapter.setOrderType(orderType);
        adapter.swapCursor(null);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getSupportMenuInflater();
        inflater.inflate(R.menu.homemenu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.SEARCH_OPTION:
            onSearchRequested();
            return true;
        case R.id.STOCK_OPTION:
            final Intent intent = new Intent(this, StockActivity.class);
            startActivity(intent);
            return true;
        case R.id.mining_session_option:
            final Intent miningSessionIntent = new Intent(this, MiningSessionActivity.class);
            startActivity(miningSessionIntent);
            return true;
        case R.id.PARAMETERS_OPTION:
            final Intent paramIntent = new Intent(this, ParameterActivity.class);
            startActivity(paramIntent);
            return true;
        case R.id.HELP_OPTION:
            SimpleOkDialog<?> dialog = new SimpleOkDialog<Object>(R.string.itemlisttitle,
                    R.layout.help_favorite_list);
            dialog.show(getSupportFragmentManager(), "helpDialog");
            return true;

        default:
            return false;
        }
    }

    private void showResults() {
        orderType = OrderType.NAME_ALPHA;
        adapter = new AmazingSimpleCursorAdapter(this, R.layout.row, dataColumnsSearch, viewIDs,
                SearchManager.SUGGEST_COLUMN_TEXT_1);

        adapter.setViewBinder(new ItemListViewBinder());

        ListView itemListView = (ListView) findViewById(R.id.ITEM_LIST);
        itemListView.setAdapter(adapter);
        itemListView.setOnItemClickListener(new ItemOnItemListClickListener());
        getSupportLoaderManager().restartLoader(SEARCH_LOADER_ID, null, this);
    }
}