Java tutorial
/****************************************************************************** * Copyright (c) 2015 - 2016 Henrik Sandklef * * This file is part of Coach Assistant * * 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 com.sandklef.coachapp.fragments; import android.app.Activity; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import com.sandklef.coachapp.misc.Log; import com.sandklef.coachapp.model.Member; import com.sandklef.coachapp.model.Team; import com.sandklef.coachapp.storage.LocalStorage; import com.sandklef.coachapp.storage.Storage; import com.sandklef.coachapp.storage.StorageNoClubException; import java.util.ArrayList; import coachassistant.sandklef.com.coachapp.R; public class TeamFragment extends Fragment implements AbsListView.OnItemClickListener { private ListView list; private ArrayAdapter<String> adapter; private ArrayList<String> arrayList; private TeamFragmentListener mListener; private AbsListView mListView; private ListAdapter mAdapter; private final static String LOG_TAG = TeamFragment.class.getSimpleName(); public static TeamFragment newInstance() { Log.d(LOG_TAG, " newInstance()"); TeamFragment fragment = new TeamFragment(); Bundle args = new Bundle(); fragment.setArguments(args); return fragment; } public TeamFragment() { // Required empty public constructor Log.d(LOG_TAG, LOG_TAG + "()"); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); try { Log.d(LOG_TAG, " teams: " + getActivity()); Log.d(LOG_TAG, " teams: " + Storage.getInstance().getTeams()); Log.d(LOG_TAG, " teams: " + Storage.getInstance().getTeams()); // adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(), android.R.layout.simple_spinner_item, arrayList); mAdapter = new ArrayAdapter<Team>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, Storage.getInstance().getTeams()); } catch (StorageNoClubException e) { e.printStackTrace(); } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_team, container, false); // Set the adapter mListView = (AbsListView) view.findViewById(android.R.id.list); ((AdapterView<ListAdapter>) mListView).setAdapter(mAdapter); // Set OnItemClickListener so we can be notified on item clicks mListView.setOnItemClickListener(this); // Inflate the layout for this fragment return view; } /* // TODO: Rename method, update argument and hook method into UI event public void onButtonPressed(Uri uri) { if (mListener != null) { mListener.onTeamFragmentInteraction(uri); } } */ @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (TeamFragmentListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement TeamFragmentListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { if (null != mListener) { try { Team t = Storage.getInstance().getTeams().get((int) id); Log.d(LOG_TAG, " member clicked: " + t.getUuid() + " " + t); mListener.onTeamFragmentInteraction(t); } catch (StorageNoClubException e) { e.printStackTrace(); } } } /** * This interface must be implemented by activities that contain this * fragment to allow an interaction in this fragment to be communicated * to the activity and potentially other fragments contained in that * activity. * <p/> * See the Android Training lesson <a href= * "http://developer.android.com/training/basics/fragments/communicating.html" * >Communicating with Other Fragments</a> for more information. */ public interface TeamFragmentListener { // TODO: Update argument type and name public void onTeamFragmentInteraction(Team t); } }