Android Beginner 24th Day
10_fragmentarguments
Code Crimelistfragment.java
Package com.bignerdranch.android.criminalintent;import android.content.Intent;import Android.os.Bundle;import android.support.v4.app.Fragment;import Android.support.v7.widget.LinearLayoutManager;import Android.support.v7.widget.RecyclerView;import Android.view.LayoutInflater;import Android.view.View;import Android.view.ViewGroup;import Android.widget.ImageView;import Android.widget.TextView;import java.util.List; Public classCrimelistfragmentextendsFragment {PrivateRecyclerview Mcrimerecyclerview;PrivateCrimeadapter Madapter;@Override PublicViewOncreateview(Layoutinflater inflater, ViewGroup container, Bundle savedinstancestate) {View view = Inflater.Inflate(R.Layout.fragment_crime_list, Container,false); Mcrimerecyclerview = (recyclerview) view.Findviewbyid(R.ID.Crime_recycler_view); Mcrimerecyclerview.Setlayoutmanager(New Linearlayoutmanager(getactivity()));UpdateUI();returnView }@Override Public void Onresume() {Super.Onresume();UpdateUI(); }Private void UpdateUI() {Crimelab Crimelab = Crimelab.Get(getactivity()); list<crime> crimes = Crimelab.Getcrimes();if(Madapter = =NULL) {Madapter =New Crimeadapter(crimes); Mcrimerecyclerview.Setadapter(Madapter); }Else{Madapter.notifydatasetchanged(); } }Private classCrimeholderextendsRecyclerview.Viewholder ImplementsView.Onclicklistener{PrivateCrime Mcrime;PrivateTextView Mtitletextview;PrivateTextView Mdatetextview;PrivateImageView Msolvedimageview; Public Crimeholder(Layoutinflater Inflater, viewgroup parent) {Super(Inflater.Inflate(R.Layout.List_item_crime, parent,false)); Itemview.Setonclicklistener( This); Mtitletextview = (TextView) Itemview.Findviewbyid(R.ID.Crime_title); Mdatetextview = (TextView) Itemview.Findviewbyid(R.ID.crime_date); Msolvedimageview = (ImageView) Itemview.Findviewbyid(R.ID.crime_solved); } Public void Bind(Crime Crime) {mcrime = crime; Mtitletextview.SetText(Mcrime.GetTitle()); Mdatetextview.SetText(Mcrime.getDate().toString()); Msolvedimageview.setvisibility(Crime.issolved() ? View.VISIBLE: View.GONE); }@Override Public void OnClick(View view) {Intent Intent = crimeactivity.newintent(getactivity(), Mcrime.getId());startactivity(intent); } }Private classCrimeadapterextendsRecyclerview.Adapter<CrimeHolder> {PrivateList<crime> Mcrimes; Public Crimeadapter(list<crime> crimes) {mcrimes = crimes; }@Override PublicCrimeholderOncreateviewholder(ViewGroup parent,intViewType) {Layoutinflater layoutinflater = Layoutinflater. from(getactivity());return New Crimeholder(Layoutinflater, parent); }@Override Public void Onbindviewholder(Crimeholder holder,intPosition) {Crime Crime = mcrimes.Get(position); Holder.Bind(crime); }@Override Public int GetItemCount() {returnMcrimes.size(); } }}
Android Beginner 24th Day