· 5 years ago · Feb 11, 2020, 04:42 AM
1package com.fabulous_academy.controllers.adapters;
2
3import android.annotation.SuppressLint;
4import android.app.Activity;
5import android.content.Context;
6import android.database.Cursor;
7import android.database.sqlite.SQLiteDatabase;
8import android.view.LayoutInflater;
9import android.view.MotionEvent;
10import android.view.View;
11import android.view.ViewGroup;
12import android.webkit.WebView;
13import android.widget.LinearLayout;
14import android.widget.TextView;
15import android.widget.Toast;
16
17import androidx.annotation.NonNull;
18import androidx.viewpager.widget.PagerAdapter;
19import androidx.viewpager.widget.ViewPager;
20
21import com.fabulous_academy.R;
22import com.fabulous_academy.datamodel.QuestionViewPagerModel;
23
24import java.util.List;
25
26import static android.content.Context.MODE_PRIVATE;
27
28public class QuestionViewPagerAdapter extends PagerAdapter implements View.OnClickListener {
29 LayoutInflater layoutInflater;
30 Context context;
31 int checkA = 0, checkB = 0, checkC = 0, checkD = 0, checkE = 0;
32 private List <QuestionViewPagerModel> qvList;
33 private SQLiteDatabase db;
34 private String tbl_useranswer;
35 private String exam_id;
36 private String tbl_answer;
37 private int pos;
38 private Activity mActivity;
39 private Context mContext;
40 private int currentPage;
41 private String q_id;
42
43
44 public QuestionViewPagerAdapter(List <QuestionViewPagerModel> qvList, Context context, String exam_id, Activity mActivity, Context mContext) {
45 this.qvList = qvList;
46 this.context = context;
47 this.exam_id = exam_id;
48 this.mActivity = mActivity;
49 this.mContext = mContext;
50 }
51
52 public QuestionViewPagerAdapter(int position) {
53 this.pos = position;
54 }
55
56 @Override
57 public int getCount() {
58 return qvList.size ();
59 }
60
61 @Override
62 public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
63 return view.equals ( object );
64 }
65
66 @SuppressLint("ClickableViewAccessibility")
67 @NonNull
68 @Override
69 public Object instantiateItem(@NonNull ViewGroup container, int position) {
70
71 layoutInflater = LayoutInflater.from ( context );
72 View view = layoutInflater.inflate ( R.layout.exam_single_question_layout, container, false );
73
74 LinearLayout singleopA, singleopB, singleopC, singleopD, singleopE;
75 TextView txtopA, txtopB, txtopC, txtopD, txtopE;
76
77 db = mActivity.openOrCreateDatabase ( "quizdb", MODE_PRIVATE, null );
78 db.execSQL ( "create table if not exists quiz_questions(id integer primary key autoincrement,exam_id varchar(10),q_id varchar(10),question varchar(10000)," +
79 "answer varchar(200),optiona varchar(200),optionb varchar(200),optionc varchar(200),optiond varchar(200),optione varchar(200),user_answer varchar(200)," +
80 "attempted_status varchar(20),answer_status varchar(20), image varchar(200))" );
81
82 WebView wvquestion, wvopA, wvopB, wvopC, wvopD, wvopE;
83
84 wvquestion = view.findViewById ( R.id.single_txt_question );
85 wvopA = view.findViewById ( R.id.single_btnOpt1 );
86 wvopB = view.findViewById ( R.id.single_btnOpt2 );
87 wvopC = view.findViewById ( R.id.single_btnOpt3 );
88 wvopD = view.findViewById ( R.id.single_btnOpt4 );
89 wvopE = view.findViewById ( R.id.single_btnOpt5 );
90
91 singleopA = view.findViewById ( R.id.single_a_layout );
92 singleopB = view.findViewById ( R.id.single_b_layout );
93 singleopC = view.findViewById ( R.id.single_c_layout );
94 singleopD = view.findViewById ( R.id.single_d_layout );
95 singleopE = view.findViewById ( R.id.single_e_layout );
96
97 txtopA = view.findViewById ( R.id.single_option_a );
98 txtopB = view.findViewById ( R.id.single_option_b );
99 txtopC = view.findViewById ( R.id.single_option_c );
100 txtopD = view.findViewById ( R.id.single_option_d );
101 txtopE = view.findViewById ( R.id.single_option_e );
102
103 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
104
105 wvquestion.loadDataWithBaseURL ( null, qvList.get ( position ).getQuestion ()+qvList.get ( position ).getQ_id (), "text/html", "utf-8", null );
106 wvopA.loadDataWithBaseURL ( null, qvList.get ( position ).getOptiona (), "text/html", "utf-8", null );
107 wvopB.loadDataWithBaseURL ( null, qvList.get ( position ).getOptionb (), "text/html", "utf-8", null );
108 wvopC.loadDataWithBaseURL ( null, qvList.get ( position ).getOptionc (), "text/html", "utf-8", null );
109 wvopD.loadDataWithBaseURL ( null, qvList.get ( position ).getOptiond (), "text/html", "utf-8", null );
110 wvopE.loadDataWithBaseURL ( null, qvList.get ( position ).getOptione (), "text/html", "utf-8", null );
111
112// q_id = qvList.get ( pos ).getQ_id ();
113// tbl_answer = qvList.get ( position ).getAnswer ();
114// Toast.makeText ( mContext,""+pos,Toast.LENGTH_SHORT ).show ();
115
116 String qry = "select * from quiz_questions where exam_id='" + exam_id + "' and q_id='"+q_id+"'";
117 Cursor cursor = db.rawQuery ( qry, null );
118 if (cursor.moveToFirst ()) {
119 String uAns = cursor.getString ( cursor.getColumnIndex ( "user_answer" ) );
120 switch (uAns) {
121 case "optiona":
122 layoutAcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
123 break;
124 case "optionb":
125 layoutBcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
126 break;
127 case "optionc":
128 layoutCcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
129 break;
130 case "optiond":
131 layoutDcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
132 break;
133 case "optione":
134 layoutEcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
135 break;
136 }
137 } else {
138 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
139 }
140
141 wvopA.setOnTouchListener ( new View.OnTouchListener () {
142 @Override
143 public boolean onTouch(View v, MotionEvent event) {
144 tbl_useranswer = "optiona";
145 if (event.getAction () == MotionEvent.ACTION_UP) {
146 if (checkA == 0) {
147 optionSelection ();
148 layoutAcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
149 checkA = 1;
150 checkB = 0;
151 checkC = 0;
152 checkD = 0;
153 checkE = 0;
154 } else {
155 tbl_useranswer = "";
156 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
157 optionDeSelection ();
158 checkA = 0;
159 }
160 return true;
161 }
162 return true;
163 }
164 } );
165
166 wvopB.setOnTouchListener ( new View.OnTouchListener () {
167 @Override
168 public boolean onTouch(View v, MotionEvent event) {
169 tbl_useranswer = "optionb";
170 if (event.getAction () == MotionEvent.ACTION_UP) {
171 if (checkB == 0) {
172 optionSelection ();
173 layoutBcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
174 checkA = 0;
175 checkB = 1;
176 checkC = 0;
177 checkD = 0;
178 checkE = 0;
179 } else {
180 tbl_useranswer = "";
181 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
182 optionDeSelection ();
183 checkB = 0;
184 }
185 return true;
186 }
187 return true;
188 }
189 } );
190
191 wvopC.setOnTouchListener ( new View.OnTouchListener () {
192 @Override
193 public boolean onTouch(View v, MotionEvent event) {
194 tbl_useranswer = "optionc";
195 if (event.getAction () == MotionEvent.ACTION_UP) {
196
197 if (checkC == 0) {
198 optionSelection ();
199 layoutCcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
200 checkA = 0;
201 checkB = 0;
202 checkC = 1;
203 checkD = 0;
204 checkE = 0;
205 } else {
206 tbl_useranswer = "";
207 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
208 optionDeSelection ();
209 checkC = 0;
210 }
211 return false;
212 }
213 return true;
214 }
215 } );
216
217 wvopD.setOnTouchListener ( new View.OnTouchListener () {
218 @Override
219 public boolean onTouch(View v, MotionEvent event) {
220 tbl_useranswer = "optiond";
221 if (event.getAction () == MotionEvent.ACTION_UP) {
222 if (checkD == 0) {
223 optionSelection ();
224 layoutDcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
225 checkA = 0;
226 checkB = 0;
227 checkC = 0;
228 checkD = 1;
229 checkE = 0;
230 } else {
231 tbl_useranswer = "";
232 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
233 optionDeSelection ();
234 checkD = 0;
235 }
236 return true;
237 }
238 return true;
239 }
240 } );
241
242 wvopE.setOnTouchListener ( new View.OnTouchListener () {
243
244 @Override
245 public boolean onTouch(View v, MotionEvent event) {
246 tbl_useranswer = "optione";
247 if (event.getAction () == MotionEvent.ACTION_UP) {
248
249 if (checkE == 0) {
250 optionSelection ();
251 layoutEcolor ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
252 checkA = 0;
253 checkB = 0;
254 checkC = 0;
255 checkD = 0;
256 checkE = 1;
257 } else {
258 tbl_useranswer = "";
259 initialText ( singleopA, singleopB, singleopC, singleopD, singleopE, txtopA, txtopB, txtopC, txtopD, txtopE );
260 optionDeSelection ();
261 checkE = 0;
262 }
263 return true;
264 }
265 return true;
266 }
267 } );
268
269
270
271// tbl_answer = qvList.get ( position ).getAnswer ();
272// q_id = qvList.get ( position ).getQ_id ();
273// Toast.makeText ( mContext,""+q_id,Toast.LENGTH_SHORT ).show ();
274
275
276
277
278 singleopA.setOnClickListener ( this );
279 singleopB.setOnClickListener ( this );
280 singleopC.setOnClickListener ( this );
281 singleopD.setOnClickListener ( this );
282 singleopE.setOnClickListener ( this );
283 txtopA.setOnClickListener ( this );
284 txtopB.setOnClickListener ( this );
285 txtopC.setOnClickListener ( this );
286 txtopD.setOnClickListener ( this );
287 txtopE.setOnClickListener ( this );
288
289 ViewPager vp = (ViewPager) container;
290 vp.addView ( view, 0 );
291 return view;
292 }
293
294 @Override
295 public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
296 ViewPager vp = (ViewPager) container;
297 View view = (View) object;
298 vp.removeView ( view );
299 }
300
301 public void checkedGivenQuestion(int position) {
302
303
304 q_id = qvList.get ( position ).getQ_id ();
305 currentPage = position;
306 Toast.makeText ( mContext, "" + q_id, Toast.LENGTH_SHORT ).show ();
307 }
308
309 private void optionSelection() {
310
311 if (tbl_answer.equals ( tbl_useranswer )) {
312 String qry = "update quiz_questions set user_answer='" + tbl_useranswer + "',attempted_status=1,answer_status='1' where exam_id='" + exam_id + "' and q_id='" + q_id + "'";
313 db.execSQL ( qry );
314 Toast.makeText ( mContext, "update", Toast.LENGTH_SHORT ).show ();
315 } else {
316 String qry = "update quiz_questions set user_answer='" + tbl_useranswer + "',attempted_status=1,answer_status='0' where exam_id='" + exam_id + "' and q_id='" + q_id + "'";
317 db.execSQL ( qry );
318 }
319 }
320
321 private void optionDeSelection() {
322
323 String qry = "update quiz_questions set user_answer='" + tbl_useranswer + "',attempted_status=0,answer_status='2' where exam_id='" + exam_id + "' and q_id='" + q_id + "'";
324 db.execSQL ( qry );
325
326 }
327
328 private void layoutAcolor(LinearLayout singleopA, LinearLayout singleopB, LinearLayout singleopC, LinearLayout singleopD, LinearLayout singleopE,
329 TextView txtopA, TextView txtopB, TextView txtopC, TextView txtopD, TextView txtopE) {
330
331 txtopA.setBackgroundResource ( R.drawable.option_selected );
332 txtopB.setBackgroundResource ( R.drawable.option_bg );
333 txtopC.setBackgroundResource ( R.drawable.option_bg );
334 txtopD.setBackgroundResource ( R.drawable.option_bg );
335 txtopE.setBackgroundResource ( R.drawable.option_bg );
336 singleopA.setBackgroundResource ( R.drawable.covred_bg );
337 singleopB.setBackgroundResource ( R.drawable.border );
338 singleopC.setBackgroundResource ( R.drawable.border );
339 singleopD.setBackgroundResource ( R.drawable.border );
340 singleopE.setBackgroundResource ( R.drawable.border );
341 }
342
343 public void layoutBcolor(LinearLayout singleopA, LinearLayout singleopB, LinearLayout singleopC, LinearLayout singleopD, LinearLayout singleopE,
344 TextView txtopA, TextView txtopB, TextView txtopC, TextView txtopD, TextView txtopE) {
345
346 txtopA.setBackgroundResource ( R.drawable.option_bg );
347 txtopB.setBackgroundResource ( R.drawable.option_selected );
348 txtopC.setBackgroundResource ( R.drawable.option_bg );
349 txtopD.setBackgroundResource ( R.drawable.option_bg );
350 txtopE.setBackgroundResource ( R.drawable.option_bg );
351 singleopA.setBackgroundResource ( R.drawable.border );
352 singleopB.setBackgroundResource ( R.drawable.covred_bg );
353 singleopC.setBackgroundResource ( R.drawable.border );
354 singleopD.setBackgroundResource ( R.drawable.border );
355 singleopE.setBackgroundResource ( R.drawable.border );
356 }
357
358 public void layoutCcolor(LinearLayout singleopA, LinearLayout singleopB, LinearLayout singleopC, LinearLayout singleopD, LinearLayout singleopE,
359 TextView txtopA, TextView txtopB, TextView txtopC, TextView txtopD, TextView txtopE) {
360
361
362 txtopA.setBackgroundResource ( R.drawable.option_bg );
363 txtopB.setBackgroundResource ( R.drawable.option_bg );
364 txtopC.setBackgroundResource ( R.drawable.option_selected );
365 txtopD.setBackgroundResource ( R.drawable.option_bg );
366 txtopE.setBackgroundResource ( R.drawable.option_bg );
367 singleopA.setBackgroundResource ( R.drawable.border );
368 singleopB.setBackgroundResource ( R.drawable.border );
369 singleopC.setBackgroundResource ( R.drawable.covred_bg );
370 singleopD.setBackgroundResource ( R.drawable.border );
371 singleopE.setBackgroundResource ( R.drawable.border );
372
373 }
374
375 public void layoutDcolor(LinearLayout singleopA, LinearLayout singleopB, LinearLayout singleopC, LinearLayout singleopD, LinearLayout singleopE,
376 TextView txtopA, TextView txtopB, TextView txtopC, TextView txtopD, TextView txtopE) {
377
378 txtopA.setBackgroundResource ( R.drawable.option_bg );
379 txtopB.setBackgroundResource ( R.drawable.option_bg );
380 txtopC.setBackgroundResource ( R.drawable.option_bg );
381 txtopD.setBackgroundResource ( R.drawable.option_selected );
382 txtopE.setBackgroundResource ( R.drawable.option_bg );
383 singleopA.setBackgroundResource ( R.drawable.border );
384 singleopB.setBackgroundResource ( R.drawable.border );
385 singleopC.setBackgroundResource ( R.drawable.border );
386 singleopD.setBackgroundResource ( R.drawable.covred_bg );
387 singleopE.setBackgroundResource ( R.drawable.border );
388 }
389
390 public void layoutEcolor(LinearLayout singleopA, LinearLayout singleopB, LinearLayout singleopC, LinearLayout singleopD, LinearLayout singleopE,
391 TextView txtopA, TextView txtopB, TextView txtopC, TextView txtopD, TextView txtopE) {
392
393 txtopA.setBackgroundResource ( R.drawable.option_bg );
394 txtopB.setBackgroundResource ( R.drawable.option_bg );
395 txtopC.setBackgroundResource ( R.drawable.option_bg );
396 txtopD.setBackgroundResource ( R.drawable.option_bg );
397 txtopE.setBackgroundResource ( R.drawable.option_selected );
398 singleopA.setBackgroundResource ( R.drawable.border );
399 singleopB.setBackgroundResource ( R.drawable.border );
400 singleopC.setBackgroundResource ( R.drawable.border );
401 singleopD.setBackgroundResource ( R.drawable.border );
402 singleopE.setBackgroundResource ( R.drawable.covred_bg );
403 }
404
405 public void initialText(LinearLayout singleopA, LinearLayout singleopB, LinearLayout singleopC, LinearLayout singleopD, LinearLayout singleopE,
406 TextView txtopA, TextView txtopB, TextView txtopC, TextView txtopD, TextView txtopE) {
407
408 txtopA.setBackgroundResource ( R.drawable.option_bg );
409 txtopB.setBackgroundResource ( R.drawable.option_bg );
410 txtopC.setBackgroundResource ( R.drawable.option_bg );
411 txtopD.setBackgroundResource ( R.drawable.option_bg );
412 txtopE.setBackgroundResource ( R.drawable.option_bg );
413 singleopA.setBackgroundResource ( R.drawable.border );
414 singleopB.setBackgroundResource ( R.drawable.border );
415 singleopC.setBackgroundResource ( R.drawable.border );
416 singleopD.setBackgroundResource ( R.drawable.border );
417 singleopE.setBackgroundResource ( R.drawable.border );
418 }
419
420 @Override
421 public void onClick(View v) {
422 switch (v.getId ()) {
423 case R.id.single_a_layout:
424 case R.id.single_option_a:
425 Toast.makeText ( mContext, "op1", Toast.LENGTH_SHORT ).show ();
426 tbl_useranswer = "optiona";
427 if (checkA == 0) {
428 optionSelection ();
429// layoutAcolor ();
430 checkA = 1;
431 checkB = 0;
432 checkC = 0;
433 checkD = 0;
434 checkE = 0;
435 } else {
436 tbl_useranswer = "";
437// initialText ();
438 optionDeSelection ();
439 checkA = 0;
440 }
441 break;
442
443 case R.id.single_b_layout:
444 case R.id.single_option_b:
445 Toast.makeText ( mContext, "op2", Toast.LENGTH_SHORT ).show ();
446 tbl_useranswer = "optionb";
447 if (checkB == 0) {
448 optionSelection ();
449// layoutBcolor ();
450 checkA = 0;
451 checkB = 1;
452 checkC = 0;
453 checkD = 0;
454 checkE = 0;
455 } else {
456 tbl_useranswer = "";
457// initialText ();
458 optionDeSelection ();
459 checkB = 0;
460 }
461 break;
462
463 case R.id.single_c_layout:
464 case R.id.single_option_c:
465 Toast.makeText ( mContext, "op3", Toast.LENGTH_SHORT ).show ();
466 tbl_useranswer = "optionc";
467 if (checkC == 0) {
468 optionSelection ();
469// layoutCcolor ();
470 checkA = 0;
471 checkB = 0;
472 checkC = 1;
473 checkD = 0;
474 checkE = 0;
475 } else {
476 tbl_useranswer = "";
477// initialText ();
478 optionDeSelection ();
479 checkC = 0;
480 }
481 break;
482
483 case R.id.single_d_layout:
484 case R.id.single_option_d:
485 Toast.makeText ( mContext, "op4", Toast.LENGTH_SHORT ).show ();
486 tbl_useranswer = "optiond";
487 if (checkD == 0) {
488 optionSelection ();
489// layoutDcolor ();
490 checkA = 0;
491 checkB = 0;
492 checkC = 0;
493 checkD = 1;
494 checkE = 0;
495 } else {
496 tbl_useranswer = "";
497// initialText ();
498 optionDeSelection ();
499 checkD = 0;
500 }
501 break;
502
503 case R.id.single_e_layout:
504 case R.id.single_option_e:
505 Toast.makeText ( mContext, "op5", Toast.LENGTH_SHORT ).show ();
506 tbl_useranswer = "optione";
507 if (checkE == 0) {
508 optionSelection ();
509// layoutEcolor ();
510 checkA = 0;
511 checkB = 0;
512 checkC = 0;
513 checkD = 0;
514 checkE = 1;
515 } else {
516 tbl_useranswer = "";
517// initialText ();
518 optionDeSelection ();
519 checkE = 0;
520 }
521 break;
522 }
523 }
524}