Android Open Source - Joetz-Android-V2 Persons Fragment






From Project

Back to project page Joetz-Android-V2.

License

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.

Java Source Code

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());
    }
}




Java Source Code List

com.example.jens.myapplication.ApplicationTest.java
com.example.jens.myapplication.account.FragmentAccount.java
com.example.jens.myapplication.account.LoginDialogActivity.java
com.example.jens.myapplication.account.LoginFragment.java
com.example.jens.myapplication.account.PersonsFragment.java
com.example.jens.myapplication.account.ProfileFragment.java
com.example.jens.myapplication.account.RegisterDialogActivity.java
com.example.jens.myapplication.account.RegisterFragment.java
com.example.jens.myapplication.account.TabsPagerAdapter.java
com.example.jens.myapplication.activities.ActivityDetailActivity.java
com.example.jens.myapplication.activities.ActivityDetailFragment.java
com.example.jens.myapplication.activities.FragmentListActivities.java
com.example.jens.myapplication.activities.JoetzActivitiesAdapter.java
com.example.jens.myapplication.activities.JoetzActivity.java
com.example.jens.myapplication.activities.StartActivity.java
com.example.jens.myapplication.apimanager.ApiConnection.java
com.example.jens.myapplication.apimanager.CancellableTask.java
com.example.jens.myapplication.apimanager.JSONParsingTask.java
com.example.jens.myapplication.apimanager.PostRequestTask.java
com.example.jens.myapplication.apimanager.RequestParams.java
com.example.jens.myapplication.apimanager.SimpleRequestTask.java
com.example.jens.myapplication.apimanager.filter.PersonFilter.java
com.example.jens.myapplication.apimanager.manager.ActivityManager.java
com.example.jens.myapplication.apimanager.manager.AuthToken.java
com.example.jens.myapplication.apimanager.manager.BookingManager.java
com.example.jens.myapplication.apimanager.manager.CampManager.java
com.example.jens.myapplication.apimanager.manager.ImageManager.java
com.example.jens.myapplication.apimanager.manager.LoginManager.java
com.example.jens.myapplication.apimanager.manager.ProfileManager.java
com.example.jens.myapplication.apimanager.manager.RegisterManager.java
com.example.jens.myapplication.booking.AddPersonActivity.java
com.example.jens.myapplication.booking.BookingActivity.java
com.example.jens.myapplication.booking.BookingBondFragment.java
com.example.jens.myapplication.booking.BookingContactFragment.java
com.example.jens.myapplication.booking.BookingExtraFragment.java
com.example.jens.myapplication.booking.BookingParticipantsFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailActivity.java
com.example.jens.myapplication.bookingHistory.BookingHistoryDetailFragment.java
com.example.jens.myapplication.bookingHistory.BookingHistoryOverviewFragment.java
com.example.jens.myapplication.bookingHistory.BookingsAdapter.java
com.example.jens.myapplication.calendar.FragmentCalendar.java
com.example.jens.myapplication.calendar.KalenderAdapter.java
com.example.jens.myapplication.calendar.KalenderItem.java
com.example.jens.myapplication.camps.CampAdapter.java
com.example.jens.myapplication.camps.CampDetailActivity.java
com.example.jens.myapplication.camps.CampFilterActivity.java
com.example.jens.myapplication.camps.FilterFragment.java
com.example.jens.myapplication.camps.FragmentListCamps.java
com.example.jens.myapplication.camps.ImageActivity.java
com.example.jens.myapplication.camps.ImagePagerFragment.java
com.example.jens.myapplication.camps.InfoFragment.java
com.example.jens.myapplication.camps.PhotoFragment.java
com.example.jens.myapplication.camps.RecyclerItemClickListener.java
com.example.jens.myapplication.camps.SquareImageView.java
com.example.jens.myapplication.camps.TabsPagerAdapter.java
com.example.jens.myapplication.domain.BookingStatus.java
com.example.jens.myapplication.domain.Booking.java
com.example.jens.myapplication.domain.Camp.java
com.example.jens.myapplication.domain.PeriodCategory.java
com.example.jens.myapplication.domain.Person.java
com.example.jens.myapplication.domain.UserRole.java
com.example.jens.myapplication.domain.User.java
com.example.jens.myapplication.domain.binding.PersonValidatorBinding.java
com.example.jens.myapplication.domain.binding.UserValidatorBinding.java
com.example.jens.myapplication.domain.binding.ValidatorBinding.java
com.example.jens.myapplication.domain.converter.ErrorConverter.java
com.example.jens.myapplication.domain.converter.MyStringParser.java
com.example.jens.myapplication.domain.converter.UnixDateTimeConverter.java
com.example.jens.myapplication.domain.validator.PersonValidator.java
com.example.jens.myapplication.domain.validator.UserValidator.java
com.example.jens.myapplication.domain.validator.Validator.java
com.example.jens.myapplication.home.AanbevolenAdapter.java
com.example.jens.myapplication.home.HomeFragment.java
com.example.jens.myapplication.home.ImageSlideAdapter.java
com.example.jens.myapplication.home.Reclame.java
com.example.jens.myapplication.lib.RangeSeekBar.java
com.example.jens.myapplication.main.MainActivity.java
com.example.jens.myapplication.main.NavDrawerAdapter.java
com.example.jens.myapplication.main.NavDrawerItem.java
com.example.jens.myapplication.main.NavMenuItem.java
com.example.jens.myapplication.main.NavMenuSection.java
com.example.jens.myapplication.sam.JoetzApplication.java
com.example.jens.myapplication.sam.MyFragment.java
com.example.jens.myapplication.sam.MySaveableFragment.java
com.example.jens.myapplication.util.AESEncryption.java
com.example.jens.myapplication.util.ActivityUtils.java
com.example.jens.myapplication.util.BindingUtils.java
com.example.jens.myapplication.util.DateTimeStringConverter.java
com.example.jens.myapplication.util.JsonUtils.java
com.example.jens.myapplication.util.MyMonths.java
com.example.jens.myapplication.util.StringUtils.java
com.example.jens.myapplication.view.MyLinearLayout.java