Java tutorial
/* This file is part of My Expenses. * My Expenses 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. * * My Expenses 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 My Expenses. If not, see <http://www.gnu.org/licenses/>. */ package org.totschnig.myexpenses.activity; import org.totschnig.myexpenses.MyApplication; import org.totschnig.myexpenses.R; import org.totschnig.myexpenses.fragment.TaskExecutionFragment; import org.totschnig.myexpenses.model.PaymentMethod; import org.totschnig.myexpenses.model.Template; import org.totschnig.myexpenses.model.Transaction; import org.totschnig.myexpenses.provider.DatabaseConstants; import com.actionbarsherlock.view.Menu; import com.actionbarsherlock.view.MenuInflater; import android.content.Intent; import android.database.Cursor; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.view.ContextMenu; import android.view.View; import android.view.ContextMenu.ContextMenuInfo; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; public class ManageMethods extends ProtectedFragmentActivity implements OnItemClickListener { Cursor mMethodsCursor; @Override public void onCreate(Bundle savedInstanceState) { setTheme(MyApplication.getThemeId()); super.onCreate(savedInstanceState); setContentView(R.layout.manage_methods); setTitle(R.string.pref_manage_methods_title); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getSupportMenuInflater(); inflater.inflate(R.menu.methods, menu); super.onCreateOptionsMenu(menu); return true; } @Override public boolean dispatchCommand(int command, Object tag) { if (command == R.id.CREATE_COMMAND) { Intent i = new Intent(this, MethodEdit.class); startActivity(i); return true; } return super.dispatchCommand(command, tag); } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent i = new Intent(this, MethodEdit.class); i.putExtra(DatabaseConstants.KEY_ROWID, id); startActivity(i); } /* (non-Javadoc) * @see android.app.Activity#onCreateContextMenu(android.view.ContextMenu, android.view.View, android.view.ContextMenu.ContextMenuInfo) */ @Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) { super.onCreateContextMenu(menu, v, menuInfo); AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo; //predefined methods can not be deleted PaymentMethod method; method = PaymentMethod.getInstanceFromDb(info.id); if (method.predef == null) { menu.add(0, R.id.DELETE_COMMAND, 0, R.string.menu_delete); } } @Override public boolean onContextItemSelected(android.view.MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo(); switch (item.getItemId()) { case R.id.DELETE_COMMAND: if (Transaction.countPerMethod(info.id) > 0) { Toast.makeText(this, getString(R.string.not_deletable_mapped_transactions), Toast.LENGTH_LONG) .show(); } else if (Template.countPerMethod(info.id) > 0) { Toast.makeText(this, getString(R.string.not_deletable_mapped_templates), Toast.LENGTH_LONG).show(); } else { FragmentManager fm = getSupportFragmentManager(); fm.beginTransaction().add(TaskExecutionFragment .newInstance(TaskExecutionFragment.TASK_DELETE_PAYMENT_METHOD, info.id, null), "ASYNC_TASK") .commit(); } return true; } return super.onContextItemSelected(item); } }