"The Definitive Guide to Android Programming (Third Edition)" Chapter 13 Challenge Exercise Demo

"The Definitive Guide to Android Programming (Third Edition)" Chapter 13 Challenge Exercise Demo

I've been busy for two weeks, and I'm back again, and finally relax for a while, hahahahaha, I have time to write! Let's go, challenge and practice

Question 1: Delete the crime record

Here you can directly use the remove(Object object) method of List. I will not say how to add the delete icon of the toolbar. Just like the book, put the key code directly:

public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate (savedInstanceState);
        /**
         * Tell FragmentManager that the fragment managed by the manager should receive the call instruction of the onCreateOptionsMenu() method
         */
        setHasOptionsMenu(true);
        crimeId = (UUID) getArguments().getSerializable(ARG_CRIME_ID);
        mCrime = CrimeLab.get (getActivity ()). getCrime (crimeId);
    }

Be sure to remember to add setHasOptionsMenu(true) to the onCreate method so that the onCreateOptionsMenu method can be called later

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        super.onCreateOptionsMenu(menu, inflater);
        inflater.inflate(R.menu.fragment_crime, menu);
    }

    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.delete_item:
                Crime crime = CrimeLab.get(getActivity()).getCrime(crimeId);
                CrimeLab.get(getActivity()).deleteCrime(crime);
                getActivity().finish();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

Set the click event here, get the UUID of the current item, find the corresponding crime in the List of the singleton class, and then call the delete method of CrimeLab to delete it. ):

public class CrimeLab {

    private static CrimeLab sCrimeLab;
    private List<Crime> mCrimes;

    public static CrimeLab get(Context context) {
        if (sCrimeLab == null) {
            sCrimeLab = new CrimeLab(context);
        }
        return sCrimeLab;
    }

    private CrimeLab(Context context) {
        mCrimes = new ArrayList<>();
    }

    public void addCrime(Crime c) {
        mCrimes.add(c);
    }

    public void deleteCrime(Crime c) {
        mCrimes.remove(c);
    }

    public List<Crime> getCrimes() {
        return mCrimes;
    }

    public Crime getCrime(UUID id) {
        for(Crime crime : mCrimes) {
            if (crime.getId().equals(id)) {
                return crime;
            }
        }
        return null;
    }

}

The first question is important to the above, the next question.

Question 2: Plural String Resources

This question is very strange, because the author of this book is not Chinese, I guess he didn't think it through. After putting the code of the second question, when there is only one item, the plural form is still displayed. After checking it, the language of the mobile phone should be changed. It can be displayed normally only if it is in English. This is the only thing to pay attention to in the second question.

Question 3: Empty View for RecyclerView

In this question, I directly changed the layout file, and then judged whether there was a value in the code, and then displayed different layouts, that is, through View.VISIBLE, View.GONE to achieve the display, but the title said that adding a button is convenient for Yonghe directly. Creating a new crime record doesn't feel necessary, because the toolbar itself has an add button, which is a bit redundant. Of course, adding a button to jump to CrimePagerActivity is also possible, but it doesn't make sense logically, so I didn't add. The main code of this question is as follows:

private void updataSuntitle() {
        CrimeLab crimeLab = CrimeLab.get (getActivity ());
        /**
         * Judgment statement to determine whether there is an item
         */
        int crimeSize = crimeLab.getCrimes().size();
        String subtitle = getResources().getQuantityString(R.plurals.subtitle_plural, crimeSize, crimeSize);
        if (crimeSize != 0) {
            subtitle = getResources().getQuantityString(R.plurals.subtitle_plural, crimeSize, crimeSize);
            mCrimeRecyclerview.setVisibility(View.VISIBLE);
            mTextView.setVisibility(View.GONE);
        } else {
            mCrimeRecyclerview.setVisibility(View.GONE);
            mTextView.setVisibility(View.VISIBLE);
        }

        if (!mSubtitleVisible) {
            subtitle = null;
        }

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.getSupportActionBar().setSubtitle(subtitle);
    }

The three questions are over, and I haven't written it for two weeks. There may be some shortcomings. I hope everyone can put forward more questions, thank you :)

Source link: Demo

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325586840&siteId=291194637