-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_CrimeAdapter_to_CrimeListFragment.patch
138 lines (129 loc) · 5.19 KB
/
add_CrimeAdapter_to_CrimeListFragment.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
Index: app/src/main/java/com/example/mycriminallintent/controller/fragment/CrimeListFragment.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
--- app/src/main/java/com/example/mycriminallintent/controller/fragment/CrimeListFragment.java (revision d613540f39935fcb653530411c852ba2aab7dac7)
+++ app/src/main/java/com/example/mycriminallintent/controller/fragment/CrimeListFragment.java (date 1595601584622)
@@ -1,9 +1,11 @@
package com.example.mycriminallintent.controller.fragment;
+import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
+import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
@@ -14,14 +16,18 @@
import android.widget.TextView;
import com.example.mycriminallintent.R;
+import com.example.mycriminallintent.controller.activity.CrimeDetailActivity;
import com.example.mycriminallintent.model.Crime;
import com.example.mycriminallintent.repository.CrimeRepository;
import com.example.mycriminallintent.repository.RepositoryInterface;
+import java.util.List;
+
public class CrimeListFragment extends Fragment {
private RecyclerView mRecyclerView;
private RepositoryInterface<Crime> mRepository;
+ private CrimeAdapter mCrimeAdapter;
public CrimeListFragment() {
// Required empty public constructor
@@ -31,30 +37,90 @@
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
- mRepository= CrimeRepository.getInstance();
+ mRepository = CrimeRepository.getInstance();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
- View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
+ View view = inflater.inflate(R.layout.fragment_crime_list, container, false);
findViews(view);
+ mRecyclerView.setLayoutManager((new LinearLayoutManager(getActivity())));
+ updateUI();
return view;
}
- private void findViews(View view){
- mRecyclerView=view.findViewById(R.id.recycler_view_crimes);
+
+ private void findViews(View view) {
+ mRecyclerView = view.findViewById(R.id.recycler_view_crimes);
}
- private class CrimeHolder extends RecyclerView.ViewHolder{
+
+ private class CrimeHolder extends RecyclerView.ViewHolder {
private TextView mTextViewTitle;
private TextView mTextViewDate;
private ImageView mImageViewSolved;
+
+ private Crime mCrime;
+
public CrimeHolder(@NonNull View itemView) {
super(itemView);
- mTextViewTitle=itemView.findViewById(R.id.list_row_crime_title);
- mTextViewDate=itemView.findViewById((R.id.list_row_crime_date));
- mImageViewSolved=itemView.findViewById(R.id.imgview_solved);
+ mTextViewTitle = itemView.findViewById(R.id.list_row_crime_title);
+ mTextViewDate = itemView.findViewById((R.id.list_row_crime_date));
+ mImageViewSolved = itemView.findViewById(R.id.imgview_solved);
+ itemView.setOnClickListener(new View.OnClickListener() {
+ @Override
+ public void onClick(View view) {
+ Intent intent = new Intent(getActivity(), CrimeDetailActivity.class);
+ startActivity(intent);
+ }
+ });
+ }
+
+ public void bindCrime(Crime crime) {
+ mCrime = crime;
+ mTextViewTitle.setText(crime.getTitle());
+ mTextViewDate.setText(crime.getDate().toString());
+ mImageViewSolved.setVisibility(crime.isSolved() ? View.VISIBLE : View.INVISIBLE);
+ }
+ }
+
+ private class CrimeAdapter extends RecyclerView.Adapter<CrimeHolder> {
+
+ private List<Crime> mCrimes;
+
+ public CrimeAdapter(List<Crime> crimes) {
+ mCrimes = crimes;
+ }
+
+ @NonNull
+ @Override
+ public CrimeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
+ LayoutInflater inflater = LayoutInflater.from(getActivity());
+ View view = inflater.inflate(R.layout.list_row_crime, parent, false);
+ CrimeHolder crimeHolder = new CrimeHolder(view);
+ return crimeHolder;
+ }
+
+ @Override
+ public void onBindViewHolder(@NonNull CrimeHolder holder, int position) {
+ Crime crime = mCrimes.get(position);
+ holder.bindCrime(crime);
+ }
+
+ @Override
+ public int getItemCount() {
+ return mCrimes.size();
+ }
+ }
+
+ private void updateUI() {
+ List<Crime> crimes = mRepository.getList();
+ if (mCrimeAdapter == null) {
+ mCrimeAdapter = new CrimeAdapter(crimes);
+ mRecyclerView.setAdapter(mCrimeAdapter);
+ } else {
+ mCrimeAdapter.notifyDataSetChanged();
}
}
}
\ No newline at end of file