1 |
efrain |
1 |
package com.cesams.twogetskills.fragment;
|
|
|
2 |
|
|
|
3 |
import android.content.Context;
|
|
|
4 |
import android.os.Build;
|
|
|
5 |
import android.os.Bundle;
|
|
|
6 |
import android.text.Html;
|
|
|
7 |
import android.util.Log;
|
|
|
8 |
import android.view.LayoutInflater;
|
|
|
9 |
import android.view.View;
|
|
|
10 |
import android.view.ViewGroup;
|
4 |
gabriel |
11 |
import android.widget.TextView;
|
1 |
efrain |
12 |
|
|
|
13 |
import androidx.annotation.NonNull;
|
|
|
14 |
import androidx.annotation.Nullable;
|
|
|
15 |
import androidx.fragment.app.Fragment;
|
|
|
16 |
import androidx.lifecycle.LifecycleOwner;
|
|
|
17 |
import androidx.recyclerview.widget.LinearLayoutManager;
|
|
|
18 |
import androidx.recyclerview.widget.RecyclerView;
|
|
|
19 |
|
|
|
20 |
import com.cesams.twogetskills.R;
|
|
|
21 |
import com.cesams.twogetskills.adapter.QuizMultipleAnswerListViewAdapter;
|
|
|
22 |
import com.cesams.twogetskills.entity.Answer;
|
|
|
23 |
import com.cesams.twogetskills.entity.Question;
|
|
|
24 |
import com.cesams.twogetskills.pojo.QuizResponse;
|
|
|
25 |
import com.cesams.twogetskills.skeleton.IQuizActivity;
|
|
|
26 |
|
|
|
27 |
import java.util.ArrayList;
|
|
|
28 |
import java.util.Collections;
|
|
|
29 |
import java.util.List;
|
|
|
30 |
|
7 |
gabriel |
31 |
//import com.codesgood.views.JustifiedTextView;
|
1 |
efrain |
32 |
|
|
|
33 |
public class QuizMultipleAnswerFragment extends Fragment implements LifecycleOwner {
|
|
|
34 |
private final String TAG = "C2GS - QuizMultiFrag";
|
|
|
35 |
private IQuizActivity iQuizActivity;
|
|
|
36 |
private QuizMultipleAnswerListViewAdapter adapter;
|
|
|
37 |
private RecyclerView recyclerView;
|
|
|
38 |
private Question mQuestion;
|
|
|
39 |
private ArrayList<Answer> mAnswers;
|
|
|
40 |
private List<QuizResponse> mQuizResponses;
|
7 |
gabriel |
41 |
// private JustifiedTextView justifyTextView;
|
4 |
gabriel |
42 |
private TextView titulo;
|
1 |
efrain |
43 |
|
|
|
44 |
|
|
|
45 |
// data is passed into the constructor
|
|
|
46 |
public QuizMultipleAnswerFragment (Context context) {
|
|
|
47 |
this.iQuizActivity = (IQuizActivity) context ;
|
|
|
48 |
this.mQuestion = iQuizActivity.getQuestionActive();
|
|
|
49 |
this.mQuizResponses = iQuizActivity.getQuizResponses();
|
|
|
50 |
this.mAnswers = new ArrayList<>();
|
|
|
51 |
|
|
|
52 |
}
|
|
|
53 |
|
|
|
54 |
|
|
|
55 |
@Override
|
|
|
56 |
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
|
|
57 |
|
|
|
58 |
return inflater.inflate(R.layout.fragment_quiz_multiple_answer, container, false);
|
|
|
59 |
}
|
|
|
60 |
|
|
|
61 |
@Override
|
|
|
62 |
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
|
63 |
super.onViewCreated(view, savedInstanceState);
|
|
|
64 |
|
|
|
65 |
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());
|
|
|
66 |
mAnswers = new ArrayList<>();
|
|
|
67 |
adapter = new QuizMultipleAnswerListViewAdapter(getActivity(), mQuestion, mAnswers, mQuizResponses);
|
|
|
68 |
|
|
|
69 |
|
|
|
70 |
|
4 |
gabriel |
71 |
titulo = getView().findViewById(R.id.textView5);
|
1 |
efrain |
72 |
recyclerView = (RecyclerView) getView().findViewById(R.id.list_quiz_multiple_answer);
|
|
|
73 |
recyclerView.setAdapter(adapter);
|
|
|
74 |
recyclerView.setLayoutManager( linearLayoutManager);
|
|
|
75 |
recyclerView.setHasFixedSize(true);
|
|
|
76 |
|
|
|
77 |
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
@Override
|
|
|
81 |
public void onResume() {
|
|
|
82 |
super.onResume();
|
|
|
83 |
|
|
|
84 |
Log.d(TAG, "onResume");
|
|
|
85 |
loadData();
|
|
|
86 |
}
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
|
|
|
91 |
@Override
|
|
|
92 |
public void onHiddenChanged(boolean hidden) {
|
|
|
93 |
super.onHiddenChanged(hidden);
|
|
|
94 |
|
|
|
95 |
Log.d(TAG, "onHiddenChanged : " + (hidden ? "true" : "false"));
|
|
|
96 |
|
|
|
97 |
if(!hidden) {
|
|
|
98 |
loadData();
|
|
|
99 |
}
|
|
|
100 |
}
|
|
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
|
104 |
void loadData()
|
|
|
105 |
{
|
|
|
106 |
if(adapter != null) {
|
|
|
107 |
mQuestion = iQuizActivity.getQuestionActive();
|
|
|
108 |
|
5 |
gabriel |
109 |
titulo.setText(Html.fromHtml(mQuestion.getText(), Html.FROM_HTML_MODE_COMPACT));
|
4 |
gabriel |
110 |
|
1 |
efrain |
111 |
mAnswers.clear();
|
|
|
112 |
List<Answer> answers = answers = iQuizActivity.getAnswers(mQuestion.getUuid());
|
|
|
113 |
for (int i = 0; i < answers.size(); i++) {
|
|
|
114 |
try {
|
|
|
115 |
Answer answer = (Answer) answers.get(i).clone();
|
|
|
116 |
mAnswers.add(answer);
|
|
|
117 |
} catch (CloneNotSupportedException e) {
|
|
|
118 |
|
|
|
119 |
}
|
|
|
120 |
}
|
|
|
121 |
Collections.shuffle(mAnswers);
|
|
|
122 |
|
|
|
123 |
adapter.notifyDataSetChanged();
|
|
|
124 |
}
|
|
|
125 |
}
|
|
|
126 |
}
|