1
1
package randomappsinc .com .sqlpractice .adapters ;
2
2
3
3
import android .content .Context ;
4
+ import android .support .annotation .NonNull ;
5
+ import android .support .v7 .widget .RecyclerView ;
4
6
import android .view .LayoutInflater ;
5
7
import android .view .View ;
6
8
import android .view .ViewGroup ;
7
- import android .widget .BaseAdapter ;
8
9
import android .widget .TextView ;
9
10
10
11
import butterknife .BindColor ;
11
12
import butterknife .BindView ;
12
13
import butterknife .ButterKnife ;
14
+ import butterknife .OnClick ;
13
15
import randomappsinc .com .sqlpractice .R ;
14
16
import randomappsinc .com .sqlpractice .database .QuestionServer ;
15
17
import randomappsinc .com .sqlpractice .utils .PreferencesManager ;
16
18
17
- public class QuestionsAdapter extends BaseAdapter {
19
+ public class QuestionsAdapter extends RecyclerView .Adapter <QuestionsAdapter .QuestionViewHolder > {
20
+
21
+ public interface Listener {
22
+ void onQuestionClicked (int position );
23
+ }
18
24
19
25
private String [] questionList = new String [QuestionServer .getNumQuestions ()];
20
26
private PreferencesManager preferencesManager ;
27
+ private Listener listener ;
21
28
22
- public QuestionsAdapter (Context context , String questionTemplate ) {
29
+ public QuestionsAdapter (Context context , String questionTemplate , Listener listener ) {
23
30
preferencesManager = new PreferencesManager (context );
24
31
populateList (questionTemplate );
32
+ this .listener = listener ;
25
33
}
26
34
27
35
// Fills in "Question 1, Question 2, etc..." list
@@ -31,19 +39,31 @@ private void populateList(String questionTemplate) {
31
39
}
32
40
}
33
41
34
- public int getCount ( ) {
35
- return questionList . length ;
42
+ public long getItemId ( int position ) {
43
+ return position ;
36
44
}
37
45
38
- public String getItem (int position ) {
39
- return questionList [position ];
46
+ @ NonNull
47
+ @ Override
48
+ public QuestionViewHolder onCreateViewHolder (@ NonNull ViewGroup parent , int viewType ) {
49
+ View itemView = LayoutInflater .from (parent .getContext ()).inflate (
50
+ R .layout .question_list_item ,
51
+ parent ,
52
+ false );
53
+ return new QuestionViewHolder (itemView );
40
54
}
41
55
42
- public long getItemId (int position ) {
43
- return position ;
56
+ @ Override
57
+ public void onBindViewHolder (@ NonNull QuestionViewHolder holder , int position ) {
58
+ holder .loadQuestion (position );
44
59
}
45
60
46
- class QuestionViewHolder {
61
+ @ Override
62
+ public int getItemCount () {
63
+ return questionList .length ;
64
+ }
65
+
66
+ class QuestionViewHolder extends RecyclerView .ViewHolder {
47
67
@ BindView (R .id .question_number ) TextView questionNumber ;
48
68
@ BindView (R .id .tagged_lessons ) TextView taggedLessons ;
49
69
@ BindView (R .id .completion_icon ) TextView completionIcon ;
@@ -52,6 +72,7 @@ class QuestionViewHolder {
52
72
@ BindColor (R .color .red ) int red ;
53
73
54
74
QuestionViewHolder (View view ) {
75
+ super (view );
55
76
ButterKnife .bind (this , view );
56
77
}
57
78
@@ -63,27 +84,13 @@ void loadQuestion(int position) {
63
84
completionIcon .setText (R .string .x_icon );
64
85
completionIcon .setTextColor (red );
65
86
}
66
-
67
- questionNumber .setText (getItem (position ));
87
+ questionNumber .setText (questionList [position ]);
68
88
taggedLessons .setText (QuestionServer .getQuestionServer ().getQuestion (position ).getIdeasList ());
69
89
}
70
- }
71
90
72
- public View getView (int position , View view , ViewGroup parent ) {
73
- QuestionViewHolder holder ;
74
- if (view == null ) {
75
- LayoutInflater inflater = (LayoutInflater ) parent .getContext ()
76
- .getSystemService (Context .LAYOUT_INFLATER_SERVICE );
77
- if (inflater == null ) {
78
- throw new RuntimeException ("Unable to get a layout inflater to render questions" );
79
- }
80
- view = inflater .inflate (R .layout .question_list_item , parent , false );
81
- holder = new QuestionViewHolder (view );
82
- view .setTag (holder );
83
- } else {
84
- holder = (QuestionViewHolder ) view .getTag ();
91
+ @ OnClick (R .id .parent )
92
+ void onQuestionClicked () {
93
+ listener .onQuestionClicked (getAdapterPosition ());
85
94
}
86
- holder .loadQuestion (position );
87
- return view ;
88
95
}
89
96
}
0 commit comments