1.Model
1.Crime
public class Crime {
private UUID mId;
private String mTitle;
private String mDate;
private boolean mSolved;
public Crime(){
mId = UUID.randomUUID();
Date date = new Date();
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
mDate=format.format(date);
}
public UUID getmId() {
return mId;
}
public String getmTitle() {
return mTitle;
}
public void setmTitle(String mTitle) {
this.mTitle = mTitle;
}
public String getmDate() {
return mDate;
}
public void setmDate(String mDate) {
this.mDate = mDate;
}
public boolean ismSolved() {
return mSolved;
}
public void setmSolved(boolean mSolved) {
this.mSolved = mSolved;
}
@Override
public String toString(){
return mTitle;
}
}
2.CrimeLab
public class CrimeLab {
private ArrayList<Crime> mCrimes;
private static CrimeLab sCrimeLab;
private Context mAppContext;
private CrimeLab(Context appContext){
mAppContext = appContext;
mCrimes = new ArrayList<Crime>();
for(int i=0;i<100;++i){
Crime c = new Crime();
c.setmTitle("Crime **"+i);
c.setmSolved(i%2==0);
mCrimes.add(c);
}
}
public static CrimeLab get(Context c){
if(sCrimeLab==null){
sCrimeLab = new CrimeLab(c.getApplicationContext());
}
return sCrimeLab;
}
public ArrayList<Crime> getCrimes(){
return mCrimes;
}
public Crime getCrime(UUID id){
for(Crime c : mCrimes){
if(c.getmId().equals(id))
return c;
}
return null;
}
}
2.View
1.activity_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
2.fragment_crime.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_title_label"
style="?android:listSeparatorTextViewStyle"/>
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="@string/crime_title_hint"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_details_label"
style="?android:listSeparatorTextViewStyle"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:id="@+id/crime_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
3.list_item_crime.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_title_label"
style="?android:listSeparatorTextViewStyle"/>
<EditText
android:id="@+id/crime_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:hint="@string/crime_title_hint"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/crime_details_label"
style="?android:listSeparatorTextViewStyle"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<Button
android:id="@+id/crime_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1" />
<CheckBox
android:id="@+id/crime_solved"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/crime_solved_label"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
3.Controller
1.SingleFragmentActivity
public abstract class SingleFragmentActivity extends FragmentActivity{
protected abstract Fragment createFragment();
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fragment);
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragmentContainer);
if(null == fragment){
fragment = createFragment();
fm.beginTransaction().add(R.id.fragmentContainer, fragment);
}
}
}
2.CrimeListActivity
public class CrimeListActivity extends SingleFragmentActivity {
private static final String TAG = "CrimeListActivity";
@Override
protected Fragment createFragment(){
return new CrimeListFragment();
}
}
3.CrimeListFragment
public class CrimeListFragment extends ListFragment {
private ArrayList<Crime> mCrimes;
private static final String TAG = "CrimeListFragment";
@Override
public void onCreate(Bundle savedInstanceState){
Log.d(TAG,"-----------------------onCreate is called!");
super.onCreate(savedInstanceState);
getActivity().setTitle(R.string.crimes_title);
mCrimes = CrimeLab.get(getActivity()).getCrimes();
CrimeAdapter adapter = new CrimeAdapter(mCrimes);
setListAdapter(adapter);
}
@Override
public void onListItemClick(ListView l, View v, int position, long id){
Log.d(TAG,"-----------------------onListItemClick is called!");
Crime c = ((CrimeAdapter)getListAdapter()).getItem(position);
Log.d(TAG,c.getmTitle()+" was clicked");
Intent i = new Intent(getActivity(),CrimeActivity.class);
i.putExtra(CrimeFragment.EXTRA_CRIME_ID,c.getmId());
startActivity(i);
}
private class CrimeAdapter extends ArrayAdapter<Crime>{
public CrimeAdapter(ArrayList<Crime> crimes){
super(getActivity(),0,crimes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
if(convertView == null){
convertView = getActivity().getLayoutInflater().inflate(R.layout.list_item_crime,null);
}
Crime c =getItem(position);
TextView titleTextView = (TextView)convertView.findViewById(R.id.crime_list_item_titleTextView);
titleTextView.setText(c.getmTitle());
TextView dateTextView = (TextView)convertView.findViewById(R.id.crime_list_item_dateTextView);
dateTextView.setText(c.getmDate());
CheckBox solvedCheckBox = (CheckBox)convertView.findViewById(R.id.crime_list_item_solvedCheckBox);
solvedCheckBox.setChecked(c.ismSolved());
return convertView;
}
}
@Override
public void onResume(){
super.onResume();
((CrimeAdapter)getListAdapter()).notifyDataSetChanged();
}
}
4.CrimeActivity
public class CrimeActivity extends SingleFragmentActivity {
@Override
protected Fragment createFragment(){
UUID crimeId = (UUID)getIntent().getSerializableExtra(CrimeFragment.EXTRA_CRIME_ID);
return CrimeFragment.newInstance(crimeId);
}
}
5.CrimeFragment
public class CrimeFragment extends Fragment {
private Crime mCrime;
private EditText mTitleFild;
private Button mDateButton;
private CheckBox mSolvedCheckBox;
public static final String EXTRA_CRIME_ID = "crimeid";
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
UUID crimeId = (UUID)getActivity().getIntent().getSerializableExtra(EXTRA_CRIME_ID);
mCrime = CrimeLab.get(getActivity()).getCrime(crimeId);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.fragment_crime, parent, false);
mTitleFild = (EditText)v.findViewById(R.id.crime_title);
mTitleFild.setText(mCrime.getmTitle());
mTitleFild.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
mCrime.setmTitle(s.toString());
}
@Override
public void afterTextChanged(Editable s) {
}
});
mDateButton = (Button)v.findViewById(R.id.crime_date);
mDateButton.setText(mCrime.getmDate());
mDateButton.setEnabled(false);
mSolvedCheckBox = (CheckBox)v.findViewById(R.id.crime_solved);
mSolvedCheckBox.setChecked(mCrime.ismSolved());
mSolvedCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCrime.setmSolved(isChecked);
}
});
return v;
}
public static CrimeFragment newInstance(UUID crimeId){
Bundle args = new Bundle();
args.putSerializable(EXTRA_CRIME_ID,crimeId);
CrimeFragment fragment = new CrimeFragment();
fragment.setArguments(args);
return fragment;
}
}
4.config
1.androidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="criminalintent.android.bignerdranch.com.criminalintent">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".CrimeListActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name=".CrimeActivity"
android:label="@string/app_name">
</activity>
</application>
</manifest>