Back to project page Joetz-Android-V2.
The source code is released under:
GNU General Public License
If you think the Android project Joetz-Android-V2 listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.example.jens.myapplication.booking; // ww w. j a va 2 s . c o m import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import com.example.jens.myapplication.R; import com.example.jens.myapplication.domain.Person; import com.example.jens.myapplication.sam.JoetzApplication; import com.example.jens.myapplication.sam.MyFragment; import com.example.jens.myapplication.sam.MySaveableFragment; import java.util.ArrayList; import java.util.List; public class BookingParticipantsFragment extends MyFragment implements MySaveableFragment { public static final String ARG_COUNT = "amount"; public static final String ARG_PERSON = "person"; //private static final String ARG_BUNDLE = "bundle"; private OnFragmentInteractionListener mListener; private boolean mSelfAdded = false; private List<Person> mParticipants; private LinearLayout mLlyParticipants; private TextView mTxtNoParticipants; private Button mBtnAdd; private Button mBtnBack; private Button mBtnNext; public static BookingParticipantsFragment newInstance(){ return newInstance(null); } /** * * @param bundle Bundle containing previous information of this fragment * @return */ public static BookingParticipantsFragment newInstance(Bundle bundle) { BookingParticipantsFragment fragment = new BookingParticipantsFragment(); if(bundle != null){ fragment.setArguments(bundle); } return fragment; } public BookingParticipantsFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mParticipants = new ArrayList<Person>(); Bundle bundle = null; if(getArguments() != null){ bundle = getArguments(); } if(bundle != null){ //Extract every person from the bundle int amount = bundle.getInt(ARG_COUNT); for(int i=0; i<amount; i++){ String key = ARG_PERSON + i; Person p = bundle.getParcelable(key); mParticipants.add(p); } } } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View rootView = inflater.inflate(R.layout.fragment_booking_participants, container, false); mLlyParticipants = (LinearLayout) rootView.findViewById(R.id.llyParticipants); mTxtNoParticipants = (TextView) rootView.findViewById(R.id.txtNoParticipants); if(mParticipants.size() > 0){ mTxtNoParticipants.setVisibility(View.GONE); } mBtnAdd = (Button) rootView.findViewById(R.id.btnAdd); mBtnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addParticipant(); } }); mBtnBack = (Button) rootView.findViewById(R.id.btnBack); mBtnBack.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goBack(); } }); mBtnNext = (Button) rootView.findViewById(R.id.btnNext); mBtnNext.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { goNext(); } }); //mLineBottom = rootView.findViewById(R.id.lineBottomParticipants); //mLineTop = rootView.findViewById(R.id.lineTopParticipants); for(Person p : mParticipants){ addPersonView(p); } return rootView; } /** * Adds a new person to the list * @param person The person to be added */ private void addPerson(Person person){ if(person.getId() == JoetzApplication.getContext().getLoginManager().getUser().getPerson().getId()){ mSelfAdded = true; } mParticipants.add(person); addPersonView(person); } /** * Adds the view for a new person to the list * @param person The person whose info should be displayed in the view */ private void addPersonView(final Person person){ final View newView = getActivity().getLayoutInflater() .inflate(R.layout.view_participant_list_item, null); initDataOnListItem(newView, person); mLlyParticipants.addView(newView); if(!(mTxtNoParticipants.getVisibility() == View.GONE)){ mTxtNoParticipants.setVisibility(View.GONE); } } /** * Fill the view with the info of the person and attach listeners * @param container The container of the view * @param person The person 'attached' to this view */ private void initDataOnListItem(final View container, final Person person){ TextView txtName = (TextView) container.findViewById(R.id.txtName); Button btnDelete = (Button) container.findViewById(R.id.btnDelete); txtName.setText(person.getFirstName() + " " + person.getLastName()); container.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { editParticipant(person); } }); btnDelete.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mLlyParticipants.removeView(container); mParticipants.remove(person); if(person.getId() == JoetzApplication.getContext().getLoginManager().getUser().getPerson().getId()){ mSelfAdded = false; } if(mLlyParticipants.getChildCount() < 1) { mTxtNoParticipants.setVisibility(View.VISIBLE); } } }); } /** * Go back to the previous screen (Bond) */ private void goBack(){ mListener.toBondFragment(); } /** * Go to the next screen if the validation allows it */ private void goNext(){ if(mParticipants.size() < 1){ Toast.makeText(getActivity(), R.string.error_at_least_one_participant, Toast.LENGTH_SHORT).show(); return; } mListener.onParticipantsFinished(mParticipants); } /** * Add a new person to the list of participants */ private void addParticipant(){ addExistingPerson(); } /** * Start an activity to create a new participant */ private void createNewParticipant(){ Intent i = new Intent(getActivity(), AddPersonActivity.class); i.putExtra(AddPersonActivity.EXTRA_REQUEST_CODE, AddPersonActivity.REQUEST_NEW); startActivityForResult(i, AddPersonActivity.REQUEST_NEW); } /** * Show a dialog with the list of 'my persons' attached to the user, also has an option * to create a new participant */ private void addExistingPerson(){ AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); builder.setTitle(R.string.my_persons); final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.select_dialog_item); final List<Person> mypersons = JoetzApplication.getContext().getLoginManager().getUser().getPersons(); if(!mSelfAdded){ adapter.add(" + " + getString(R.string.add_me)); } adapter.add(" + " + getString(R.string.new_person)); final List<Person> realAdded = new ArrayList<Person>(); aLoop: for(Person p : mypersons){ for(Person partyCipant : mParticipants){ if(partyCipant.getId() == p.getId()){ continue aLoop; } } realAdded.add(p); adapter.add(p.getFirstName() + " " + p.getLastName()); } builder.setNegativeButton(R.string.cancel, null); builder.setAdapter(adapter, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { if(which == 0 && !mSelfAdded){ addMyself(); return; } else if(which == 1 && !mSelfAdded || which == 0 && mSelfAdded){ //If "New person" is chosen at the top of the list createNewParticipant(); return; } int subtract = mSelfAdded ? 1 : 2; addPerson(realAdded.get(which - subtract).clone()); } }); builder.create().show(); } /** * Start the activity to edit a person * @param person the person to be edited */ private void editParticipant(Person person){ int index = mParticipants.indexOf(person); Intent i = new Intent(getActivity(), AddPersonActivity.class); i.putExtra(AddPersonActivity.EXTRA_PERSON, person); i.putExtra(AddPersonActivity.EXTRA_INDEX, index); i.putExtra(AddPersonActivity.EXTRA_REQUEST_CODE, AddPersonActivity.REQUEST_EDIT); startActivityForResult(i, AddPersonActivity.REQUEST_EDIT); } /** * Called when the activity to edit a person has finished successfully. <br /> * Updates the user information in the list * @param person The new person information * @param index The index in the list of the person that was edited */ public void onEditedPerson(Person person, int index){ mParticipants.set(index, person); View view = mLlyParticipants.getChildAt(index); initDataOnListItem(view, person); } /** * Add the user himself to the list of participants */ private void addMyself(){ addPerson(JoetzApplication.getContext().getLoginManager().getUser().getPerson()); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { //super.onActivityResult(requestCode, resultCode, data); if(resultCode != AddPersonActivity.RESULT_OK) { return; } Person p = data.getParcelableExtra(AddPersonActivity.EXTRA_PERSON); /*switch(requestCode){ case REQUEST_NEW: addPerson(p); break; case REQUEST_EDIT:{ onEditedPerson() } }*/ if(requestCode == AddPersonActivity.REQUEST_NEW){ addPerson(p); }else if(requestCode == AddPersonActivity.REQUEST_EDIT){ int index = data.getExtras().getInt(AddPersonActivity.EXTRA_INDEX); onEditedPerson(p, index); } } @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); passBundleToParent(); mListener = null; } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); //outState.putBundle(ARG_BUNDLE, getBundle()); } @Override public void passBundleToParent(){ int size = mParticipants.size(); Bundle bundle = new Bundle(); bundle.putInt(ARG_COUNT, size); if(size > 0){ for(int i=0; i<size; i++){ String key = ARG_PERSON + i; // example: "Person1" or "Person5" bundle.putParcelable(key, mParticipants.get(i)); } } mListener.passParticipantsBundle(bundle); } public interface OnFragmentInteractionListener { /** * Return to bond fragment */ public void toBondFragment(); /** * Called when the "next" button has been clicked * @param participants The list of all the participants that were added */ public void onParticipantsFinished(List<Person> participants); /** * Saves the bundle with the information about the participants fragment * @param bundle */ public void passParticipantsBundle(Bundle bundle); } }