I am working on an application that works as a city guide. It takes the data from a MySql database, it is divided by categories of each thing.
But I wanted to try to do it also by zones. I do not know if you understand. It would be like applying a double filter.
I added a Spinner al toolbar
to select the zone and then they can select the category they want.
But the truth is that I could not think of how to do it! haha, the only way I thought is to create another category that is called North Zone for example and the locals of that area put them in their respective category and in North Zone appears in both.
Then from the spinner, select the north zone and show the locals. But obviously when you select a special category, all of them will appear to me. : s Could someone help me? this is my ActivityMain.java
package copparoni.rosarioteviste;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.SearchView;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.ArrayAdapter;
import android.widget.RelativeLayout;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.nostra13.universalimageloader.core.ImageLoader;
import copparoni.rosarioteviste.data.AppConfig;
import copparoni.rosarioteviste.data.DatabaseHandler;
import copparoni.rosarioteviste.data.SharedPref;
import copparoni.rosarioteviste.fragment.FragmentCategory;
import copparoni.rosarioteviste.utils.Tools;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.Toast;
public class ActivityMain extends AppCompatActivity {
Spinner zonas;
String[] datos = {"Zona Norte","Zona Sur","Zona Centro","Zona Este"};
//for ads
private InterstitialAd mInterstitialAd;
private ImageLoader imgloader = ImageLoader.getInstance();
public ActionBar actionBar;
public Toolbar toolbar;
private int cat[];
private Toolbar toolbar2=null;
private String[] category=null;
private FloatingActionButton fab;
private SearchView searchView;
private MenuItem searchItem;
private NavigationView navigationView;
private DatabaseHandler db;
private SharedPref sharedPref;
private RelativeLayout nav_header_lyt;
public Fragment fragment = null;
public Bundle bundle = new Bundle();
static ActivityMain activityMain;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
category = getResources().getStringArray(R.array.category);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
SpinnerAdapter spinnerAdapter = ArrayAdapter.createFromResource(getApplicationContext(), R.array.category, R.layout.spinner_dropdown_item);
Spinner navigationSpinner = new Spinner(getSupportActionBar().getThemedContext());
navigationSpinner.setAdapter(spinnerAdapter);
toolbar.addView(navigationSpinner, 0);
navigationSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
switch (position) {
case 1:
Toast.makeText(ActivityMain.this,
"you selected: " + cat[18],
Toast.LENGTH_SHORT).show();
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
if(!imgloader.isInited()) Tools.initImageLoader(this);
fab = (FloatingActionButton) findViewById(R.id.fab);
db = new DatabaseHandler(this);
sharedPref = new SharedPref(this);
prepareAds();
initToolbar();
initDrawerMenu();
prepareImageLoader();
cat = getResources().getIntArray(R.array.id_category);
// first drawer view
onItemSelected(R.id.nav_all);
actionBar.setTitle(getString(R.string.title_nav_all));
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
toogleSearchView(searchItem.isVisible());
}
});
Tools.cekConnection(getApplicationContext(), ((View)findViewById(R.id.frame_content)));
// for system bar in lollipop
Tools.systemBarLolipop(this);
}
private void initToolbar(){
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
Tools.setActionBarColor(this, actionBar);
}
private void initDrawerMenu(){
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){
@Override
public void onDrawerOpened(View drawerView) {
updateFavoritesCounter(navigationView, R.id.nav_favorites, db.getFavoritesSize());
toogleSearchView(true);
showInterstitial();
super.onDrawerOpened(drawerView);
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
actionBar.setTitle(item.getTitle().toString());
return onItemSelected(item.getItemId());
}
});
// navigation header
View nav_header = navigationView.getHeaderView(0);
nav_header_lyt = (RelativeLayout) nav_header.findViewById(R.id.nav_header_lyt);
nav_header_lyt.setBackgroundColor(Tools.colorBrighter(sharedPref.getThemeColorInt()));
(nav_header.findViewById(R.id.menu_nav_setting)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivitySetting.class);
startActivity(i);
}
});
(nav_header.findViewById(R.id.menu_nav_map)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityMaps.class);
startActivity(i);
}
});
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (!drawer.isDrawerOpen(GravityCompat.START)) {
drawer.openDrawer(GravityCompat.START);
} else {
doExitApp();
}
}
private void toogleSearchView(boolean open){
if(open){
searchItem.setVisible(false);
searchView.onActionViewCollapsed();
fab.setImageResource(R.drawable.abc_ic_search_api_mtrl_alpha);
hideKeyboard();
}else {
searchItem.setVisible(true);
searchView.onActionViewExpanded();
fab.setImageResource(R.drawable.abc_ic_clear_mtrl_alpha);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_activity_main, menu);
searchItem = menu.findItem(R.id.action_search);
searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
searchView.setIconified(false);
searchView.setQueryHint(getString(R.string.search_toolbar_hint));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String s) {
return false;
}
@Override
public boolean onQueryTextChange(String s) {
try {
FragmentCategory.filterAdapter(s);
} catch (Exception e) {}
return true;
}
});
searchView.onActionViewCollapsed();
searchItem.setVisible(false);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i = new Intent(getApplicationContext(), ActivitySetting.class);
startActivity(i);
}else if(id == R.id.action_rate){
Tools.rateAction(ActivityMain.this);
}else if(id == R.id.action_about){
Tools.aboutAction(ActivityMain.this);
}
return super.onOptionsItemSelected(item);
}
public boolean onItemSelected(int id) {
// Handle navigation view item clicks here.
switch (id) {
//sub menu
case R.id.nav_all:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, -1);
break;
// favorites
case R.id.nav_favorites:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, -2);
break;
case R.id.nav_featured:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[18]);
break;
case R.id.nav_tour:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[0]);
break;
case R.id.nav_food:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[1]);
break;
case R.id.nav_hotels:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[2]);
break;
case R.id.nav_ent:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[3]);
break;
case R.id.nav_sport:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[4]);
break;
case R.id.nav_shop:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[5]);
break;
case R.id.nav_transport:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[6]);
break;
case R.id.nav_religion:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[7]);
break;
case R.id.nav_public:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[8]);
break;
case R.id.nav_money:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[9]);
break;
case R.id.nav_unifor:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[10]);
break;
case R.id.nav_work:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[11]);
break;
case R.id.nav_bano:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[12]);
break;
case R.id.nav_opticas:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[13]);
break;
case R.id.nav_baile:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[14]);
break;
case R.id.nav_joyeria:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[15]);
break;
case R.id.nav_ninos:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[16]);
break;
case R.id.nav_spa:
fragment = new FragmentCategory();
bundle.putInt(FragmentCategory.TAG_CATEGORY, cat[17]);
break;
default:
break;
}
fragment.setArguments(bundle);
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_content, fragment);
fragmentTransaction.commit();
//initToolbar();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private void hideKeyboard(){
View view = this.getCurrentFocus();
if (view != null) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
private long exitTime = 0;
public void doExitApp() {
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(this, R.string.press_again_exit_app, Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else {
finish();
}
}
private void prepareImageLoader(){
Tools.initImageLoader(this);
}
@Override
protected void onResume() {
if(!imgloader.isInited()) Tools.initImageLoader(this);
updateFavoritesCounter(navigationView, R.id.nav_favorites, db.getFavoritesSize());
if(actionBar != null){
Tools.setActionBarColor(this, actionBar);
// for system bar in lollipop
Tools.systemBarLolipop(this);
}
if(nav_header_lyt != null){
nav_header_lyt.setBackgroundColor(Tools.colorBrighter(sharedPref.getThemeColorInt()));
}
super.onResume();
}
private void updateFavoritesCounter(NavigationView nav, @IdRes int itemId, int count) {
TextView view = (TextView) nav.getMenu().findItem(itemId).getActionView().findViewById(R.id.counter);
view.setText(String.valueOf(count));
}
private void prepareAds(){
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id));
//prepare ads
AdRequest adRequest2 = new AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequest2);
}
/** show ads */
public void showInterstitial() {
// Show the ad if it's ready
if (AppConfig.ENABLE_ADSENSE && mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
public static ActivityMain getInstance(){
return activityMain;
}
public static void animateFab(final boolean hide) {
FloatingActionButton f_ab = (FloatingActionButton)activityMain.findViewById(R.id.fab);
int moveY = hide ? (2 * f_ab.getHeight()) : 0;
f_ab.animate().translationY(moveY).setStartDelay(100).setDuration(400).start();
}
}