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.account; //from w w w . j av a 2s . co m import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; import com.example.jens.myapplication.R; import com.example.jens.myapplication.apimanager.manager.ProfileManager; import com.example.jens.myapplication.booking.AddPersonActivity; import com.example.jens.myapplication.domain.Person; import com.example.jens.myapplication.sam.JoetzApplication; import com.example.jens.myapplication.sam.MyFragment; import java.util.List; /** * Created by Jens on 13/11/2014. */ public class PersonsFragment extends MyFragment { //public static final String ARG_PERSONS = "argpersons"; private Button mBtnAdd; private LinearLayout mLlyPersons; private TextView mTxtNoPersons; private List<Person> mPersons; public static PersonsFragment newInstance(){ PersonsFragment f = new PersonsFragment(); return f; } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //Restart the application if the user is not logged in JoetzApplication.getContext().restartIfLoggedOut(); mPersons = JoetzApplication.getContext().getLoginManager().getUser().getPersons(); } @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_my_persons, container, false); mTxtNoPersons = (TextView) rootView.findViewById(R.id.txtNoPersons); mLlyPersons = (LinearLayout) rootView.findViewById(R.id.llyPersons); mBtnAdd = (Button) rootView.findViewById(R.id.btnAdd); mBtnAdd.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { addPerson(); } }); initViews(); return rootView; } /** * Initialize the views of the persons in the list */ public void initViews(){ mLlyPersons.removeAllViews(); for(Person person : mPersons){ addPersonView(person); } if(mLlyPersons.getChildCount() == 0){ mTxtNoPersons.setVisibility(View.VISIBLE); } } /** * Open the activity to add a new person */ private void addPerson(){ Intent i = new Intent(getActivity(), AddPersonActivity.class); i.putExtra(AddPersonActivity.EXTRA_TYPE, AddPersonActivity.TYPE_PERSONS); //use getParentFragment() because this is a nested fragment getParentFragment().startActivityForResult(i, AddPersonActivity.REQUEST_NEW); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { if(resultCode != Activity.RESULT_OK){ return; } if(requestCode == AddPersonActivity.REQUEST_NEW){ //Person was added to the user during addPerson, could be implemented better TODO mPersons = JoetzApplication.getContext() .getLoginManager().getUser().getPersons(); initViews(); }else if(requestCode == AddPersonActivity.REQUEST_EDIT){ //Extract the person that was returned from the addPerson activity Person p = data.getParcelableExtra(AddPersonActivity.EXTRA_PERSON); int index = data.getIntExtra(AddPersonActivity.EXTRA_INDEX, 0); editPerson(p, index); } } /** * Open the addPerson activity with the given person * @param p Person to be edited * @param index Index at which the person is located in your list */ private void openEditPerson(Person p, int index){ Intent i = new Intent(getActivity(), AddPersonActivity.class); //Let activity know u want to add/edit a PERSON of your user //AddPersonActivity can also be used for participants i.putExtra(AddPersonActivity.EXTRA_TYPE, AddPersonActivity.TYPE_PERSONS); i.putExtra(AddPersonActivity.EXTRA_PERSON, p); i.putExtra(AddPersonActivity.EXTRA_INDEX, index); //Request code to let the activity u know u want to edit, not add i.putExtra(AddPersonActivity.EXTRA_REQUEST_CODE, AddPersonActivity.REQUEST_EDIT); getParentFragment().startActivityForResult(i, AddPersonActivity.REQUEST_EDIT); } /** * Show dialog to confirm removal of a person * @param person Person to be removed * @param index Index of the person to be removed (required to remove from view list) TODO refactor? */ private void askRemovePerson(final Person person, final int index){ new AlertDialog.Builder(getActivity()) .setMessage(getString(R.string.really_delete_person, person.getFirstName() + " " + person.getLastName())) .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { removePerson(person, index); } }) .setNegativeButton(R.string.no, null) .create().show(); } /** * Actually remove the person, should only be called after confirmation dialog "yes" was pressed * @param person Person to be removed * @param index Index of the person to be removed */ private void removePerson(final Person person, int index){ mPersons.remove(person); mLlyPersons.removeViewAt(index); //API call to remove the person //Not waiting for confirmation ProfileManager.removePerson(person.getId(), null); //Initialize listeners again int count = mLlyPersons.getChildCount(); for(int i=0; i<count; i++){ final int ind = i; View v = mLlyPersons.getChildAt(i); Button b = (Button) v.findViewById(R.id.btnRemove); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { askRemovePerson(person, ind); } }); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openEditPerson(person, ind); } }); } if(mLlyPersons.getChildCount() == 0){ mTxtNoPersons.setVisibility(View.VISIBLE); }else{ mTxtNoPersons.setVisibility(View.GONE); } } /** * Edit a person * @param person The person to be edited * @param index The index of the person to be edited in the view */ private void editPerson(final Person person, final int index){ mPersons.set(index, person); View v = mLlyPersons.getChildAt(index); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openEditPerson(person, index); } }); v.findViewById(R.id.btnRemove).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { askRemovePerson(person, index); } }); fillPersonView(v, person); } /** * Add the view for this person * @param person Person that was added */ private void addPersonView(final Person person){ View v = getActivity().getLayoutInflater().inflate(R.layout.item_persons_person, null); Button b = (Button) v.findViewById(R.id.btnRemove); final int ind = mLlyPersons.getChildCount(); b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { askRemovePerson(person, ind); } }); v.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { openEditPerson(person, ind); } }); fillPersonView(v, person); mLlyPersons.addView(v); mTxtNoPersons.setVisibility(View.GONE); } /** * Fill a view with the person information * @param view View to be filled * @param person Person whose info has to be inserted in the view */ private void fillPersonView(View view, Person person){ TextView txtName = (TextView) view.findViewById(R.id.txtName); txtName.setText(person.getFirstName() + " " + person.getLastName()); } }