Java tutorial
/* * 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.eoidb.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.support.v4.widget.SimpleCursorAdapter; 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.eoidb.EOIDBConst; import fr.eoidb.R; import fr.eoidb.activity.listener.ItemOnItemListClickListener; import fr.eoidb.activity.util.ItemAttributeListViewBinder; import fr.eoidb.activity.util.compat.ActionBarActivity; import fr.eoidb.db.bean.ColumnsNames; import fr.eoidb.db.bean.Item; import fr.eoidb.util.DbUtil; public class ItemAttributesListActivity extends ActionBarActivity implements LoaderManager.LoaderCallbacks<Cursor> { private static final String[] dataColumns = { ColumnsNames.AttibuteType.COLUMN_NAME_ICON_ID, ColumnsNames.AttibuteType.COLUMN_NAME_NAME, ColumnsNames.TypeAttibutes.COLUMN_NAME_VALUE_FLOAT, ColumnsNames.EveUnits.NAME_ALIAS }; private static final String[] dataColumnsSearch = { Item._ID, SearchManager.SUGGEST_COLUMN_TEXT_1 }; private static final int[] viewIDs = { R.id.attribute_icon, R.id.attribute_name, R.id.attribute_value, R.id.attribute_unit }; protected final static int LOADER_ID = EOIDBConst.getNextLoaderIdSequence(); protected final static int SEARCH_LOADER_ID = EOIDBConst.getNextLoaderIdSequence(); private String query; // private SearchView searchView; private SimpleCursorAdapter adapter; private long marketGroupId; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.market_group_list); 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, ItemInfoActivity.class); itemIntent.setData(intent.getData()); startActivity(itemIntent); finish(); } else { // Creates the backing adapter for the ListView. adapter = new SimpleCursorAdapter(this, R.layout.attibute_row, null, dataColumns, viewIDs, SimpleCursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER); adapter.setViewBinder(new ItemAttributeListViewBinder()); ListView itemListView = (ListView) findViewById(R.id.market_group_list); //itemListView.setOnItemClickListener(new MarketGroupOnItemListClickListener()); // Sets the ListView's adapter to be the cursor adapter that was just created. itemListView.setAdapter(adapter); getSupportLoaderManager().initLoader(LOADER_ID, null, this); } } @Override public Loader<Cursor> onCreateLoader(int id, Bundle args) { if (id == LOADER_ID) { return new CursorLoader(this, getIntent().getData(), new String[] { ColumnsNames.AttibuteType._ID, ColumnsNames.AttibuteType.COLUMN_NAME_NAME, ColumnsNames.AttibuteType.COLUMN_NAME_ICON_ID, ColumnsNames.TypeAttibutes.COLUMN_NAME_VALUE_FLOAT, ColumnsNames.TypeAttibutes.COLUMN_NAME_VALUE_FLOAT, "eu." + ColumnsNames.EveUnits.COLUMN_NAME_NAME + " AS " + ColumnsNames.EveUnits.NAME_ALIAS }, null, null, null); } 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.swapCursor(data); findViewById(android.R.id.empty).setVisibility(View.GONE); findViewById(R.id.market_group_list).setVisibility(View.VISIBLE); } else { findViewById(R.id.market_group_list).setVisibility(View.GONE); findViewById(android.R.id.empty).setVisibility(View.VISIBLE); } } @Override public void onLoaderReset(Loader<Cursor> loader) { 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; default: return false; } } private void showResults() { ListView itemListView = (ListView) findViewById(R.id.market_group_list); itemListView.setAdapter(adapter); itemListView.setOnItemClickListener(new ItemOnItemListClickListener()); getSupportLoaderManager().restartLoader(SEARCH_LOADER_ID, null, this); } }