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.EncounterAdapter; import de.uni_koblenz_landau.apow.helper.ListViewItem; import de.uni_koblenz_landau.apow.tasks.EncounterListInterface; import de.uni_koblenz_landau.apow.tasks.EncounterListTask; import de.uni_koblenz_landau.apow.tasks.TaskActivity; import de.uni_koblenz_landau.apow.tasks.TaskActivityReference; /** * Fragment for showing a list of encounters. * * @author Martin Landua * */ public class EncounterListFragment extends Fragment implements EncounterListInterface, TaskActivity { // Constants public static final String ARG_PATIENT_ID = "patient_id"; public static final String ARG_TITLE = "title"; private static final String ARG_ENCOUNTERS = "encounters"; // Tasks private EncounterListTask mEncounterListTask; // UI Variables private int mPatientID; private List<ListViewItem> mEncounters; // UI references private ListView mListView; /** * Empty constructor for Instantiation */ public EncounterListFragment() { } @Override public void onCreate(Bundle savedInstanceState) { setHasOptionsMenu(true); super.onCreate(savedInstanceState); getActivity().setTitle(getArguments().getString(ARG_TITLE)); // Re-attach Tasks. TaskActivityReference.getInstance().attach(EncounterListTask.TASK_ID, this); if (getArguments().containsKey(ARG_PATIENT_ID)) { mPatientID = getArguments().getInt(ARG_PATIENT_ID); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.encounter_list_fragment, container, false); // Create UI references. mListView = (ListView) view.findViewById(R.id.encounter_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(), ObsListActivity.class); intent.putExtra(ObsListFragment.ARG_ENCOUNTER_ID, (int) id); intent.putExtra(ObsListFragment.ARG_TITLE, item.getField2()); getActivity().startActivity(intent); } }); // Restore UI from saved instance or load data. if (savedInstanceState != null) { mEncounters = savedInstanceState.getParcelableArrayList(ARG_ENCOUNTERS); EncounterAdapter adapter = new EncounterAdapter(this.getActivity(), mEncounters); mListView.setAdapter(adapter); } else { loadEncounters(); } return view; } @Override public void onDestroy() { super.onDestroy(); // Detach tasks for preventing memory leaks. TaskActivityReference.getInstance().detach(EncounterListTask.TASK_ID); } @Override public void onSaveInstanceState(Bundle savedInstanceState) { savedInstanceState.putParcelableArrayList(ARG_ENCOUNTERS, (ArrayList<ListViewItem>) mEncounters); super.onSaveInstanceState(savedInstanceState); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.encounter_list, menu); super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onOptionsItemSelected(MenuItem item) { int id = item.getItemId(); if (id == android.R.id.home) { NavUtils.navigateUpFromSameTask(getActivity()); return true; } if (id == R.id.encounter_list_action_add) { Intent intent = new Intent(getActivity(), EncounterDetailActivity.class); intent.putExtra(EncounterDetailFragment.ARG_ENCOUNTER_ID, -1); intent.putExtra(EncounterDetailFragment.ARG_PATIENT_ID, mPatientID); intent.putExtra(ObsDetailFragment.ARG_TITLE, getActivity().getResources().getString(R.string.encounter_detail_new_encounter_title)); getActivity().startActivity(intent); return true; } return false; } /** * Starts task to load encounters. */ public void loadEncounters() { // Abort if task is already running. if (mEncounterListTask != null) { return; } // Start task. mEncounterListTask = new EncounterListTask(); TaskActivityReference.getInstance().addTask(EncounterListTask.TASK_ID, mEncounterListTask, this); mEncounterListTask.execute(mPatientID); } /** * Called by EncounterListTask, when loading is finished. */ @Override public void onEncountersFetched(List<ListViewItem> encounters) { mEncounterListTask = null; // If result is not empty, update UI, else show error message. if (encounters != null) { mEncounters = encounters; EncounterAdapter adapter = new EncounterAdapter(this.getActivity(), mEncounters); mListView.setAdapter(adapter); } else { Toast.makeText(getActivity(), R.string.error_database, Toast.LENGTH_SHORT).show(); } } }