11package com .vogella .android .retrofitstackoverflow ;
22
3+ import android .app .Activity ;
34import android .content .Intent ;
45import android .os .Bundle ;
5- import android .support .v7 .app .AppCompatActivity ;
6+ import android .support .v7 .widget .LinearLayoutManager ;
7+ import android .support .v7 .widget .RecyclerView ;
68import android .util .Log ;
79import android .view .View ;
810import android .widget .AdapterView ;
1416import com .google .gson .Gson ;
1517import com .google .gson .GsonBuilder ;
1618
17- import java .io .IOException ;
19+ import java .util .ArrayList ;
20+ import java .util .List ;
1821
1922import okhttp3 .ResponseBody ;
2023import retrofit2 .Call ;
2326import retrofit2 .Retrofit ;
2427import retrofit2 .converter .gson .GsonConverterFactory ;
2528
26- public class MainActivity extends AppCompatActivity {
29+ public class MainActivity extends Activity implements View . OnClickListener {
2730
2831 private StackOverflowAPI stackoverflowAPI ;
2932 private String token ;
30- private static final String key = "yourKey" ;
3133
32- private Button upvoteButton ;
3334 private Button authenticateButton ;
3435
3536 private Spinner questionsSpinner ;
36- private Spinner answersSpinner ;
37+ private RecyclerView recyclerView ;
3738
3839 protected void onCreate (Bundle savedInstanceState ) {
3940 super .onCreate (savedInstanceState );
@@ -44,7 +45,7 @@ protected void onCreate(Bundle savedInstanceState) {
4445 @ Override
4546 public void onItemSelected (AdapterView <?> parent , View view , int position , long id ) {
4647 Question question = (Question ) parent .getAdapter ().getItem (position );
47- stackoverflowAPI .getAnswersForQuestion (question .getQuestionId () ).enqueue (answersCallback );
48+ stackoverflowAPI .getAnswersForQuestion (question .questionId ).enqueue (answersCallback );
4849 }
4950
5051 @ Override
@@ -53,10 +54,11 @@ public void onNothingSelected(AdapterView<?> parent) {
5354 }
5455 });
5556
56- answersSpinner = (Spinner ) findViewById (R .id .answers_spinner );
57-
5857 authenticateButton = (Button ) findViewById (R .id .authenticate_button );
59- upvoteButton = (Button ) findViewById (R .id .upvote_button );
58+
59+ recyclerView = (RecyclerView ) findViewById (R .id .list );
60+ recyclerView .setHasFixedSize (true );
61+ recyclerView .setLayoutManager (new LinearLayoutManager (MainActivity .this ));
6062
6163 createStackoverflowAPI ();
6264 stackoverflowAPI .getQuestions ().enqueue (questionsCallback );
@@ -65,34 +67,37 @@ public void onNothingSelected(AdapterView<?> parent) {
6567 @ Override
6668 protected void onResume () {
6769 super .onResume ();
68- if (token != null ){
70+ if (token != null ) {
6971 authenticateButton .setEnabled (false );
70- upvoteButton .setEnabled (true );
7172 }
7273 }
7374
7475 private void createStackoverflowAPI () {
7576 Gson gson = new GsonBuilder ()
7677 .setDateFormat ("yyyy-MM-dd'T'HH:mm:ssZ" )
7778 .create ();
79+
7880 Retrofit retrofit = new Retrofit .Builder ()
7981 .baseUrl (StackOverflowAPI .BASE_URL )
8082 .addConverterFactory (GsonConverterFactory .create (gson ))
8183 .build ();
84+
8285 stackoverflowAPI = retrofit .create (StackOverflowAPI .class );
8386 }
8487
85- public void onClick (View view ) {
86- switch (view .getId ()) {
88+ @ Override
89+ public void onClick (View v ) {
90+ switch (v .getId ()) {
91+ case android .R .id .text1 :
92+ if (token != null ) {
93+ stackoverflowAPI .postUpvoteOnAnswer ((Integer ) v .getTag (), token , getString (R .string .key ), "stackoverflow.com" , false , "default" ).enqueue (upvoteCallback );
94+ } else {
95+ Toast .makeText (this , "You need to authenticate first" , Toast .LENGTH_LONG ).show ();
96+ }
97+ break ;
8798 case R .id .authenticate_button :
8899 startActivityForResult (new Intent (this , WebViewActivity .class ), 1 );
89100 break ;
90- case R .id .upvote_button :
91- Answer selectedAnswer = (Answer ) answersSpinner .getSelectedItem ();
92- if (selectedAnswer != null ) {
93- stackoverflowAPI .postUpvoteOnAnswer (selectedAnswer .getAnswerId (), token , key , "stackoverflow.com" , false , "default" ).enqueue (upvoteCallback );
94- }
95- break ;
96101 }
97102 }
98103
@@ -103,38 +108,39 @@ protected void onActivityResult(int requestCode, int resultCode, Intent data) {
103108 }
104109 }
105110
106- Callback <QuestionsList > questionsCallback = new Callback <QuestionsList >() {
111+ Callback <ListWrapper < Question >> questionsCallback = new Callback <ListWrapper < Question > >() {
107112 @ Override
108- public void onResponse (Call <QuestionsList > call , Response <QuestionsList > response ) {
113+ public void onResponse (Call <ListWrapper < Question >> call , Response <ListWrapper < Question > > response ) {
109114 if (response .isSuccessful ()) {
110- QuestionsList questionList = response .body ();
111- ArrayAdapter <Question > arrayAdapter = new ArrayAdapter <Question >(MainActivity .this , android .R .layout .simple_spinner_dropdown_item , questionList . getItems (). toArray ( new Question [ questionList . getItems (). size ()]) );
115+ ListWrapper < Question > questions = response .body ();
116+ ArrayAdapter <Question > arrayAdapter = new ArrayAdapter <Question >(MainActivity .this , android .R .layout .simple_spinner_dropdown_item , questions . items );
112117 questionsSpinner .setAdapter (arrayAdapter );
113118 } else {
114119 Log .d ("QuestionsCallback" , "Code: " + response .code () + " Message: " + response .message ());
115120 }
116121 }
117122
118123 @ Override
119- public void onFailure (Call <QuestionsList > call , Throwable t ) {
124+ public void onFailure (Call <ListWrapper < Question > > call , Throwable t ) {
120125 t .printStackTrace ();
121126 }
122127 };
123128
124- Callback <AnswersList > answersCallback = new Callback <AnswersList >() {
129+ Callback <ListWrapper < Answer >> answersCallback = new Callback <ListWrapper < Answer > >() {
125130 @ Override
126- public void onResponse (Call <AnswersList > call , Response <AnswersList > response ) {
131+ public void onResponse (Call <ListWrapper < Answer >> call , Response <ListWrapper < Answer > > response ) {
127132 if (response .isSuccessful ()) {
128- AnswersList answersList = response .body ();
129- ArrayAdapter <Answer > arrayAdapter = new ArrayAdapter <Answer >(MainActivity .this , android .R .layout .simple_spinner_dropdown_item , answersList .getItems ().toArray (new Answer [answersList .getItems ().size ()]));
130- answersSpinner .setAdapter (arrayAdapter );
133+ List <Object > data = new ArrayList <>();
134+ data .add (questionsSpinner .getSelectedItem ());
135+ data .addAll (response .body ().items );
136+ recyclerView .setAdapter (new RecyclerViewAdapter (data , MainActivity .this ));
131137 } else {
132138 Log .d ("QuestionsCallback" , "Code: " + response .code () + " Message: " + response .message ());
133139 }
134140 }
135141
136142 @ Override
137- public void onFailure (Call <AnswersList > call , Throwable t ) {
143+ public void onFailure (Call <ListWrapper < Answer > > call , Throwable t ) {
138144 t .printStackTrace ();
139145 }
140146 };
@@ -145,6 +151,7 @@ public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
145151 if (response .isSuccessful ()) {
146152 Toast .makeText (MainActivity .this , "Upvote successful" , Toast .LENGTH_LONG ).show ();
147153 } else {
154+ Log .d ("QuestionsCallback" , "Code: " + response .code () + " Message: " + response .message ());
148155 Toast .makeText (MainActivity .this , "You already upvoted this answer" , Toast .LENGTH_LONG ).show ();
149156 }
150157 }
0 commit comments