Java tutorial
/** * Apow - a mobile EHR Management System for low-resource environments * in developing countries, exemplified by rural Ghana * Copyright (C) 2014 Martin Landua * * 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 de.uni_koblenz_landau.apow; import java.util.ArrayList; import java.util.List; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.NavUtils; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; import de.uni_koblenz_landau.apow.db.ObsAdapter; import de.uni_koblenz_landau.apow.dialogs.DeleteDialogFragment; import de.uni_koblenz_landau.apow.dialogs.DeleteDialogListener; import de.uni_koblenz_landau.apow.helper.ListViewItem; import de.uni_koblenz_landau.apow.tasks.DataUpdateInterface; import de.uni_koblenz_landau.apow.tasks.EncounterDeleteTask; import de.uni_koblenz_landau.apow.tasks.ObsListInterface; import de.uni_koblenz_landau.apow.tasks.ObsListTask; import de.uni_koblenz_landau.apow.tasks.TaskActivity; import de.uni_koblenz_landau.apow.tasks.TaskActivityReference; /** * Fragment for showing a list of observations. * * @author Martin Landua * */ public class ObsListFragment extends Fragment implements ObsListInterface, DataUpdateInterface, TaskActivity, DeleteDialogListener { // Constants public static final String ARG_ENCOUNTER_ID = "encounter_id"; public static final String ARG_TITLE = "title"; private static final String DELETE_DIALOG_ID = "deletedialog"; private static final String ARG_OBS = "obs"; // Tasks private ObsListTask mObsListTask; private EncounterDeleteTask mEncounterDeleteTask; // UI Variables private int mEncounterID; private List<ListViewItem> mObs; private String mTitle; // UI references private ListView mListView; private DeleteDialogFragment mDeleteDialog; /** * Empty constructor for Instantiation */ public ObsListFragment() { } @Override public void onCreate(Bundle savedInstanceState) { setHasOptionsMenu(true); super.onCreate(savedInstanceState); // Re-attach Tasks. TaskActivityReference.getInstance().attach(EncounterDeleteTask.TASK_ID, this); TaskActivityReference.getInstance().attach(ObsListTask.TASK_ID, this); if (getArguments().containsKey(ARG_ENCOUNTER_ID)) { mEncounterID = getArguments().getInt(ARG_ENCOUNTER_ID); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.obs_list_fragment, container, false); // Create UI references. mListView = (ListView) view.findViewById(R.id.obs_list_fragment_listview); mListView.setEmptyView(view.findViewById(android.R.id.empty)); mListView.getEmptyView().setVisibility(ListView.GONE); mListView.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { ListViewItem item = (ListViewItem) parent.getItemAtPosition(position); Intent intent = new Intent(getActivity(), ObsDetailActivity.class); intent.putExtra(ObsDetailFragment.ARG_OBS_ID, (int) id); intent.putExtra(ObsDetailFragment.ARG_ENCOUNTER_ID, mEncounterID); intent.putExtra(ObsDetailFragment.ARG_TITLE, item.getField1()); getActivity().startActivity(intent); } }); // Restore UI from saved instance or load data. if (savedInstanceState != null) { mDeleteDialog = (DeleteDialogFragment) getFragmentManager().findFragmentByTag(DELETE_DIALOG_ID); if (mDeleteDialog != null) { mDeleteDialog.setListener(this); } mTitle = savedInstanceState.getString(ARG_TITLE); getActivity().setTitle(mTitle); mObs = savedInstanceState.getParcelableArrayList(ARG_OBS); ObsAdapter adapter = new ObsAdapter(this.getActivity(), mObs); mListView.setAdapter(adapter); } else { mTitle = getArguments().getString(ARG_TITLE); loadObs(mTitle); } return view; } @Override public void onDestroy() { super.onDestroy(); // Detach tasks for preventing memory leaks. TaskActivityReference.getInstance().detach(EncounterDeleteTask.TASK_ID); TaskActivityReference.getInstance().detach(ObsListTask.TASK_ID); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putString(ARG_TITLE, mTitle); savedInstanceState.putParcelableArrayList(ARG_OBS, (ArrayList<ListViewItem>) mObs); super.onSaveInstanceState(savedInstanceState); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.obs_list, menu); super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { Intent back = new Intent(getActivity(), EncounterListActivity.class); NavUtils.navigateUpTo(getActivity(), back); return true; } if (id == R.id.obs_list_action_add) { Intent intent = new Intent(getActivity(), ObsDetailActivity.class); intent.putExtra(ObsDetailFragment.ARG_OBS_ID, -1); intent.putExtra(ObsDetailFragment.ARG_ENCOUNTER_ID, mEncounterID); intent.putExtra(ObsDetailFragment.ARG_TITLE, getActivity().getResources().getString(R.string.obs_detail_new_obs_title)); getActivity().startActivity(intent); return true; } if (id == R.id.obs_list_action_discard) { mDeleteDialog = DeleteDialogFragment.newInstance(R.string.obs_list_delete_dialog_title); mDeleteDialog.setListener(this); mDeleteDialog.show(getActivity().getSupportFragmentManager(), DELETE_DIALOG_ID); return true; } if (id == R.id.obs_list_action_edit) { Intent intent = new Intent(getActivity(), EncounterDetailActivity.class); intent.putExtra(EncounterDetailFragment.ARG_ENCOUNTER_ID, mEncounterID); getActivity().startActivity(intent); return true; } return false; } /** * Starts task to fetch observations. * @param title title to set */ public void loadObs(String title) { // Abort if task is already running. if (mObsListTask != null) { return; } // Start task. mObsListTask = new ObsListTask(); TaskActivityReference.getInstance().addTask(ObsListTask.TASK_ID, mObsListTask, this); mObsListTask.execute(mEncounterID); // Set title. if (title != null) { mTitle = title; getActivity().setTitle(mTitle); } } /** * Called by ObsListTask, when loading is finished. */ @Override public void onObsFetched(List<ListViewItem> obs) { mObsListTask = null; // If result is not empty, update UI, else show error message. if (obs != null) { mObs = obs; ObsAdapter adapter = new ObsAdapter(this.getActivity(), mObs); mListView.setAdapter(adapter); } else { Toast.makeText(getActivity(), R.string.error_database, Toast.LENGTH_SHORT).show(); } } /** * Callback for delete dialog. */ @Override public void deleteItem(String voidReason) { // Abort if task is already running. if (mEncounterDeleteTask != null) { return; } // Start task. mEncounterDeleteTask = new EncounterDeleteTask(mEncounterID, voidReason); TaskActivityReference.getInstance().addTask(EncounterDeleteTask.TASK_ID, mEncounterDeleteTask, this); mEncounterDeleteTask.execute(); } /** * Called by delete task, when update is finished. */ @Override public void onDataUpdateFinished() { mEncounterDeleteTask = null; NavUtils.navigateUpFromSameTask(getActivity()); } }