com.zephyrteam.costituzione.SearchResultsActivity.java Source code

Java tutorial

Introduction

Here is the source code for com.zephyrteam.costituzione.SearchResultsActivity.java

Source

/*
 * Costituzione Italiana 4.0 
 * Copyright (C) 2012 
 *    Gnufabio <me@gnufabio.com>, Enrix835 <enrico.trt@gmail.com>
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

package com.zephyrteam.costituzione;

import com.zephyrteam.costituzione.fragments.SearchResultsFragment;

import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.SearchView;

public class SearchResultsActivity extends BasicActivity implements SearchView.OnQueryTextListener {
    SearchResultsFragment fragment;
    FragmentManager fm;

    @Override
    public void onCreate(Bundle instance) {
        super.onCreate(instance);
        Intent callingIntent = getIntent();

        fm = getSupportFragmentManager();
        if (callingIntent.getAction().equals(Intent.ACTION_SEARCH)) {
            String keyword = callingIntent.getStringExtra(SearchManager.QUERY);
            int category = callingIntent.getIntExtra(Constants.EXTRA_CATEGORY, -1);

            refresh(keyword, category);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.actionbar_mainscreen, menu);

        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
        searchView.setIconifiedByDefault(true);
        searchView.setOnQueryTextListener(this);

        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.mainscreen_favorites:
            Intent intent = new Intent(this, FavoritesActivity.class);
            startActivity(intent);
            break;
        }
        return true;
    }

    public void refresh(String keyword, int category) {
        fragment = SearchResultsFragment.newInstance(keyword, category);
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(android.R.id.content, fragment);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_CLOSE);
        ft.commit();
    }

    @Override
    public boolean onQueryTextChange(String newText) {
        return false;
    }

    @Override
    public boolean onQueryTextSubmit(String query) {
        refresh(query, -1);
        return true;
    }
}