Java tutorial
/* * Copyright (C) 2012 Joe AmRhein * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.arconus.dicecommander.ui.fragment; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import com.arconus.dicecommander.Application; import com.arconus.dicecommander.R; import com.arconus.dicecommander.adapters.CharPowerAdapter; import com.arconus.dicecommander.events.CharacterSelectedEvent; import com.arconus.dicecommander.interactors.PowerWizardBundler; import com.arconus.dicecommander.interactors.managers.GameCharacterManager; import com.arconus.dicecommander.model.entities.character.power.CharPower; import com.arconus.dicecommander.model.entities.character.power.CharPowerRollResult; import com.arconus.dicecommander.ui.activity.PowerWizardActivity; import com.arconus.dicecommander.ui.dialogs.PowerRollResultDialog; import com.squareup.otto.Subscribe; public class PowerCardGridFragment extends Fragment implements OnItemClickListener { private ListView listView; public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); View v = getView(); if (v != null) { listView = (ListView) getView().findViewById(R.id.power_list); listView.setEmptyView(getView().findViewById(android.R.id.empty)); listView.setOnItemClickListener(this); registerForContextMenu(listView); refreshAdapter(); } } public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.power_card_grid_fragment, container, false); } @Override public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) { CharPowerRollResult r = GameCharacterManager.getInstance().rollSelectedCharacter(position); DialogFragment newFragment = PowerRollResultDialog.newInstance(r); newFragment.show(getFragmentManager(), "dialog"); } public void refreshAdapter() { if (!GameCharacterManager.isEmpty()) { CharPowerAdapter adapter = new CharPowerAdapter(getActivity(), GameCharacterManager.getInstance().getSelectedCharacter().getCharPowers()); listView.invalidateViews(); listView.setAdapter(adapter); } } @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); AdapterContextMenuInfo adapterInfo = (AdapterContextMenuInfo) menuInfo; MenuInflater inflater = getActivity().getMenuInflater(); inflater.inflate(R.menu.power_card_list_menu, menu); menu.setHeaderTitle(GameCharacterManager.getInstance().getSelectedCharacter().getCharPowers() .get(adapterInfo.position).getName()); } @Override public boolean onContextItemSelected(MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.power_card_list_item_menu_clone: CharPower clonePower = GameCharacterManager.getInstance().getPowerFromSelectedChar(info.position); Intent cloneIntent = new Intent(getActivity(), PowerWizardActivity.class); cloneIntent.putExtra(PowerWizardActivity.EDIT_POWER_ID, -1); cloneIntent.putExtra(PowerWizardActivity.EDIT_POWER_MODEL, new PowerWizardBundler(clonePower).createBundleFromPower()); startActivity(cloneIntent); return true; case R.id.power_card_list_item_menu_edit: CharPower editPower = GameCharacterManager.getInstance().getPowerFromSelectedChar(info.position); Intent editIntent = new Intent(getActivity(), PowerWizardActivity.class); editIntent.putExtra(PowerWizardActivity.EDIT_POWER_ID, info.position); editIntent.putExtra(PowerWizardActivity.EDIT_POWER_MODEL, new PowerWizardBundler(editPower).createBundleFromPower()); startActivity(editIntent); return true; case R.id.power_card_list_item_menu_remove: GameCharacterManager.getInstance().deleteCharPowerFromSelectedCharacter(info.position); refreshAdapter(); return true; default: return super.onContextItemSelected(item); } } @Override public void onPause() { super.onPause(); Application.bus.unregister(this); } @Override public void onResume() { super.onResume(); refreshAdapter(); Application.bus.register(this); } @Subscribe public void answerCharSelectedEvent(CharacterSelectedEvent event) { refreshAdapter(); } }