outattacker

Soal

Apr 24th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 16.77 KB | None | 0 0
  1. package intivestudio.web.id.moocexam;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Arrays;
  5. import java.util.Collections;
  6. import java.util.List;
  7. import java.util.concurrent.TimeUnit;
  8.  
  9. import org.apache.http.NameValuePair;
  10. import org.apache.http.message.BasicNameValuePair;
  11. import org.json.JSONArray;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import android.annotation.SuppressLint;
  16. import android.annotation.TargetApi;
  17. import android.app.Activity;
  18. import android.app.AlertDialog;
  19. import android.app.ProgressDialog;
  20. import android.content.DialogInterface;
  21. import android.graphics.Color;
  22. import android.os.AsyncTask;
  23. import android.os.Build;
  24. import android.os.Bundle;
  25. import android.os.CountDownTimer;
  26. import android.util.Log;
  27. import android.view.LayoutInflater;
  28. import android.view.View;
  29. import android.view.View.OnClickListener;
  30. import android.widget.Button;
  31. import android.widget.EditText;
  32. import android.widget.ImageView;
  33. import android.widget.RadioButton;
  34. import android.widget.RadioGroup;
  35. import android.widget.TextView;
  36. import android.widget.Toast;
  37.  
  38. public class MainActivity extends Activity {
  39.  
  40.     TextView txtNama, txtNo, txtWaktu, txtSoal;
  41.     Button btnPrev, btnSelesai, btnNext;
  42.     RadioGroup rg;
  43.     RadioButton rb1, rb2, rb3;
  44.     ImageView img;
  45.     EditText inputNama;
  46.     int jawabanYgDiPilih[] = null;
  47.     int jawabanYgBenar[] = null;
  48.     boolean cekPertanyaan = false;
  49.     int urutanPertanyaan = 0;
  50.     List<Soal> listSoal;
  51.     JSONArray soal = null;
  52.     JSONParser jParser = new JSONParser();
  53.     CounterClass mCountDownTimer;
  54.     private ProgressDialog pDialog;
  55.     private static String url = "http://192.168.8.100/exam/soal.php";
  56.     private static String urls = "http://192.168.8.100/exam/input.php";
  57.     private static final String TAG_DAFTAR = "daftar_soal";
  58.     private static final String TAG_ID = "soal_id";
  59.     private static final String TAG_SOAL = "soal";
  60.     private static final String TAG_A = "a";
  61.     private static final String TAG_B = "b";
  62.     private static final String TAG_C = "c";
  63.     private static final String TAG_JWB = "jawaban";
  64.     private static final String TAG_GAMBAR = "gambar";
  65.     public ImageLoader imageLoader;
  66.  
  67.     @Override
  68.     protected void onCreate(Bundle savedInstanceState) {
  69.         super.onCreate(savedInstanceState);
  70.         setContentView(R.layout.activity_main);
  71.         listSoal = new ArrayList<Soal>();
  72.         imageLoader = new ImageLoader(getApplicationContext());
  73.         img = (ImageView) findViewById(R.id.imageView1);
  74.         txtNama = (TextView) findViewById(R.id.textViewNama);
  75.         txtNo = (TextView) findViewById(R.id.textViewNo);
  76.         txtWaktu = (TextView) findViewById(R.id.textViewWaktu);
  77.         txtSoal = (TextView) findViewById(R.id.textViewSoal);
  78.         btnPrev = (Button) findViewById(R.id.buttonPrev);
  79.         btnSelesai = (Button) findViewById(R.id.buttonSelesai);
  80.         btnNext = (Button) findViewById(R.id.buttonNext);
  81.         rg = (RadioGroup) findViewById(R.id.radioGroup1);
  82.         rb1 = (RadioButton) findViewById(R.id.radio0);
  83.         rb2 = (RadioButton) findViewById(R.id.radio1);
  84.         rb3 = (RadioButton) findViewById(R.id.radio2);
  85.  
  86.         btnSelesai.setOnClickListener(klikSelesai);
  87.         btnPrev.setOnClickListener(klikSebelum);
  88.         btnNext.setOnClickListener(klikBerikut);
  89.         showInputUser();
  90.     }
  91.  
  92.     private void showInputUser() {
  93.         LayoutInflater mInflater = LayoutInflater.from(this);
  94.         View v = mInflater.inflate(R.layout.input_user, null);
  95.         final AlertDialog dialog = new AlertDialog.Builder(this).create();
  96.         dialog.setView(v);
  97.         dialog.setTitle("Isikan Nama Anda");
  98.         dialog.setIcon(R.mipmap.ic_launcher);
  99.         dialog.setCancelable(false);
  100.         final Button btnOk = (Button) v.findViewById(R.id.btnOk);
  101.         inputNama = (EditText) v.findViewById(R.id.inputID);
  102.         btnOk.setOnClickListener(new View.OnClickListener() {
  103.  
  104.             @Override
  105.             public void onClick(View v) {
  106.                 if (inputNama.getText().toString().equals("")) {
  107.                     Toast.makeText(getBaseContext(), "Isikan Nama Anda",
  108.                             Toast.LENGTH_LONG).show();
  109.                 } else {
  110.                     new GetSoal().execute();
  111.                     txtNama.setText(inputNama.getText().toString());
  112.                     dialog.dismiss();
  113.                 }
  114.  
  115.             }
  116.         });
  117.  
  118.         dialog.show();
  119.  
  120.     }
  121.  
  122.     private class GetSoal extends AsyncTask<Void, Void, Void> {
  123.  
  124.         @Override
  125.         protected void onPreExecute() {
  126.             super.onPreExecute();
  127.             // Showing progress dialog
  128.             pDialog = new ProgressDialog(MainActivity.this);
  129.             pDialog.setMessage("Please wait...");
  130.             pDialog.setCancelable(false);
  131.             pDialog.show();
  132.  
  133.         }
  134.  
  135.         @Override
  136.         protected Void doInBackground(Void... arg0) {
  137.             // Creating service handler class instance
  138.             ServiceHandler sh = new ServiceHandler();
  139.             // Making a request to url and getting response
  140.             String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
  141.             Log.d("Response: ", "> " + jsonStr);
  142.             if (jsonStr != null) {
  143.                 try {
  144.                     JSONObject jsonObj = new JSONObject(jsonStr);
  145.                     // Getting JSON Array node
  146.                     soal = jsonObj.getJSONArray(TAG_DAFTAR);
  147.                     Soal s = null;
  148.                     // looping through All Contacts
  149.                     for (int i = 0; i < soal.length(); i++) {
  150.                         JSONObject c = soal.getJSONObject(i);
  151.                         s = new Soal();
  152.  
  153.                         String id = c.getString(TAG_ID);
  154.                         String soal = c.getString(TAG_SOAL);
  155.                         String a = c.getString(TAG_A);
  156.                         String b = c.getString(TAG_B);
  157.                         String cc = c.getString(TAG_C);
  158.                         String jwb = c.getString(TAG_JWB);
  159.                         String gambar = c.getString(TAG_GAMBAR);
  160.  
  161.                         s.setId(id);
  162.                         s.setSoal(soal);
  163.                         s.setA(a);
  164.                         s.setB(b);
  165.                         s.setC(cc);
  166.                         s.setJawban(jwb);
  167.                         s.setGambar(gambar);
  168.                         listSoal.add(s);
  169.  
  170.                     }
  171.                 } catch (JSONException e) {
  172.                     e.printStackTrace();
  173.                 }
  174.             } else {
  175.                 Log.e("ServiceHandler", "Couldn't get any data from the url");
  176.             }
  177.  
  178.             return null;
  179.         }
  180.  
  181.         @Override
  182.         protected void onPostExecute(Void result) {
  183.             super.onPostExecute(result);
  184.             // Dismiss the progress dialog
  185.             if (pDialog.isShowing())
  186.                 pDialog.dismiss();
  187.  
  188.             jawabanYgDiPilih = new int[listSoal.size()];
  189.             java.util.Arrays.fill(jawabanYgDiPilih, -1);
  190.             jawabanYgBenar = new int[listSoal.size()];
  191.             java.util.Arrays.fill(jawabanYgBenar, -1);
  192.             setUpSoal();
  193.         }
  194.     }
  195.  
  196.     private void setUpSoal() {
  197.         Collections.shuffle(listSoal);
  198.         tunjukanPertanyaan(0, cekPertanyaan);
  199.     }
  200.  
  201.     private void tunjukanPertanyaan(int urutan_soal_soal, boolean review) {
  202.         btnSelesai.setEnabled(false);
  203.         if(urutan_soal_soal == 0)
  204.             setUpWaktu();
  205.  
  206.         try {
  207.             rg.clearCheck();
  208.             Soal soal = new Soal();
  209.             soal = listSoal.get(urutan_soal_soal);
  210.             if (jawabanYgBenar[urutan_soal_soal] == -1) {
  211.                 jawabanYgBenar[urutan_soal_soal] = Integer.parseInt(soal
  212.                         .getJawban());
  213.             }
  214.  
  215.             String soalnya = soal.getSoal();
  216.             txtSoal.setText(soalnya);
  217.             rg.check(-1);
  218.             rb1.setTextColor(Color.WHITE);
  219.             rb2.setTextColor(Color.WHITE);
  220.             rb3.setTextColor(Color.WHITE);
  221.             imageLoader.DisplayImage(soal.getGambar(), img);
  222.             rb1.setText(soal.getA());
  223.             rb2.setText(soal.getB());
  224.             rb3.setText(soal.getC());
  225.  
  226.             Log.d("", jawabanYgDiPilih[urutan_soal_soal] + "");
  227.             if (jawabanYgDiPilih[urutan_soal_soal] == 1)
  228.                 rg.check(R.id.radio0);
  229.             if (jawabanYgDiPilih[urutan_soal_soal] == 2)
  230.                 rg.check(R.id.radio1);
  231.             if (jawabanYgDiPilih[urutan_soal_soal] == 3)
  232.                 rg.check(R.id.radio2);
  233.  
  234.             pasangLabelDanNomorUrut();
  235.  
  236.             if (urutan_soal_soal == (listSoal.size() - 1)) {
  237.                 btnNext.setEnabled(false);
  238.                 btnSelesai.setEnabled(true);
  239.             }
  240.  
  241.             if (urutan_soal_soal == 0)
  242.                 btnPrev.setEnabled(false);
  243.  
  244.             if (urutan_soal_soal > 0)
  245.                 btnPrev.setEnabled(true);
  246.  
  247.             if (urutan_soal_soal < (listSoal.size() - 1))
  248.                 btnNext.setEnabled(true);
  249.  
  250.             if (review) {
  251.                 mCountDownTimer.cancel();
  252.                 Log.d("priksa", jawabanYgDiPilih[urutan_soal_soal] + ""
  253.                         + jawabanYgBenar[urutan_soal_soal]);
  254.                 if (jawabanYgDiPilih[urutan_soal_soal] != jawabanYgBenar[urutan_soal_soal]) {
  255.                     if (jawabanYgDiPilih[urutan_soal_soal] == 1)
  256.                         rb1.setTextColor(Color.RED);
  257.                     if (jawabanYgDiPilih[urutan_soal_soal] == 2)
  258.                         rb2.setTextColor(Color.RED);
  259.                     if (jawabanYgDiPilih[urutan_soal_soal] == 3)
  260.                         rb3.setTextColor(Color.RED);
  261.                 }
  262.                 if (jawabanYgBenar[urutan_soal_soal] == 1)
  263.                     rb1.setTextColor(Color.GREEN);
  264.                 if (jawabanYgBenar[urutan_soal_soal] == 2)
  265.                     rb2.setTextColor(Color.GREEN);
  266.                 if (jawabanYgBenar[urutan_soal_soal] == 3)
  267.                     rb3.setTextColor(Color.GREEN);
  268.             }
  269.  
  270.         } catch (Exception e) {
  271.             Log.e(this.getClass().toString(), e.getMessage(), e.getCause());
  272.         }
  273.     }
  274.  
  275.  
  276.  
  277.  
  278.     private OnClickListener klikSelesai = new OnClickListener() {
  279.         public void onClick(View v) {
  280.             aturJawaban_nya();
  281.             // hitung berapa yg benar
  282.             int jumlahJawabanYgBenar = 0;
  283.             for (int i = 0; i < jawabanYgBenar.length; i++) {
  284.                 if ((jawabanYgBenar[i] != -1)
  285.                         && (jawabanYgBenar[i] == jawabanYgDiPilih[i]))
  286.                     jumlahJawabanYgBenar++;
  287.             }
  288.             AlertDialog tampilKotakAlert;
  289.             tampilKotakAlert = new AlertDialog.Builder(MainActivity.this)
  290.                     .create();
  291.             tampilKotakAlert.setTitle("Hasil");
  292.             tampilKotakAlert.setIcon(R.mipmap.ic_launcher);
  293.             tampilKotakAlert.setMessage("Score " + jumlahJawabanYgBenar * 10);
  294.  
  295.             tampilKotakAlert.setButton(AlertDialog.BUTTON_NEUTRAL, "Lagi",
  296.                     new DialogInterface.OnClickListener() {
  297.  
  298.                         public void onClick(DialogInterface dialog, int which) {
  299.                             mCountDownTimer.cancel();
  300.                             java.util.Arrays.fill(jawabanYgDiPilih, -1);
  301.                             cekPertanyaan = false;
  302.                             urutanPertanyaan = 0;
  303.                             tunjukanPertanyaan(0, cekPertanyaan);
  304.                         }
  305.                     });
  306.  
  307.             tampilKotakAlert.setButton(AlertDialog.BUTTON_NEUTRAL, "Submit",
  308.                     new DialogInterface.OnClickListener() {
  309.  
  310.                         public void onClick(DialogInterface dialog, int which) {
  311.                             new Masukkan().execute();
  312.                         }
  313.                     });
  314.  
  315.             tampilKotakAlert.setButton(AlertDialog.BUTTON_POSITIVE, "Priksa",
  316.                     new DialogInterface.OnClickListener() {
  317.  
  318.                         public void onClick(DialogInterface dialog, int which) {
  319.                             mCountDownTimer.cancel();
  320.                             cekPertanyaan = true;
  321.                             urutanPertanyaan = 0;
  322.                             tunjukanPertanyaan(0, cekPertanyaan);
  323.                         }
  324.                     });
  325.  
  326.             tampilKotakAlert.setButton(AlertDialog.BUTTON_NEGATIVE, "Keluar",
  327.                     new DialogInterface.OnClickListener() {
  328.  
  329.                         public void onClick(DialogInterface dialog, int which) {
  330.                             mCountDownTimer.cancel();
  331.                             cekPertanyaan = false;
  332.                             finish();
  333.                         }
  334.                     });
  335.  
  336.             tampilKotakAlert.show();
  337.  
  338.         }
  339.     };
  340.  
  341.     private void aturJawaban_nya() {
  342.         if (rb1.isChecked())
  343.             jawabanYgDiPilih[urutanPertanyaan] = 1;
  344.         if (rb2.isChecked())
  345.             jawabanYgDiPilih[urutanPertanyaan] = 2;
  346.         if (rb3.isChecked())
  347.             jawabanYgDiPilih[urutanPertanyaan] = 3;
  348.  
  349.         Log.d("", Arrays.toString(jawabanYgDiPilih));
  350.         Log.d("", Arrays.toString(jawabanYgBenar));
  351.  
  352.     }
  353.  
  354.     private OnClickListener klikBerikut = new OnClickListener() {
  355.         public void onClick(View v) {
  356.             aturJawaban_nya();
  357.             urutanPertanyaan++;
  358.             if (urutanPertanyaan >= listSoal.size())
  359.                 urutanPertanyaan = listSoal.size() - 1;
  360.  
  361.             tunjukanPertanyaan(urutanPertanyaan, cekPertanyaan);
  362.         }
  363.     };
  364.  
  365.     private OnClickListener klikSebelum = new OnClickListener() {
  366.         public void onClick(View v) {
  367.             aturJawaban_nya();
  368.             urutanPertanyaan--;
  369.             if (urutanPertanyaan < 0)
  370.                 urutanPertanyaan = 0;
  371.  
  372.             tunjukanPertanyaan(urutanPertanyaan, cekPertanyaan);
  373.         }
  374.     };
  375.  
  376.     private void pasangLabelDanNomorUrut() {
  377.         txtNo.setText("No. " + (urutanPertanyaan + 1)+ " dari "
  378.                 + listSoal.size() + " soal");
  379.     }
  380.  
  381.     private void setUpWaktu() {
  382.         mCountDownTimer = new CounterClass(240000, 1000);
  383.         mCountDownTimer.start();
  384.     }
  385.  
  386.     @SuppressLint("DefaultLocale")
  387.     public class CounterClass extends CountDownTimer {
  388.         public CounterClass(long millisInFuture, long countDownInterval) {
  389.             super(millisInFuture, countDownInterval);
  390.         }
  391.  
  392.         @Override
  393.         public void onFinish() {
  394.             finish();
  395.         }
  396.  
  397.         @SuppressLint("NewApi")
  398.         @TargetApi(Build.VERSION_CODES.GINGERBREAD)
  399.         @Override
  400.         public void onTick(long millisUntilFinished) {
  401.             long millis = millisUntilFinished;
  402.             String hms = String.format(
  403.                     "%02d:%02d:%02d",
  404.                     TimeUnit.MILLISECONDS.toHours(millis),
  405.                     TimeUnit.MILLISECONDS.toMinutes(millis)
  406.                             - TimeUnit.HOURS.toMinutes(TimeUnit.MILLISECONDS
  407.                             .toHours(millis)),
  408.                     TimeUnit.MILLISECONDS.toSeconds(millis)
  409.                             - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS
  410.                             .toMinutes(millis)));
  411.             txtWaktu.setText(hms);
  412.         }
  413.     }
  414.  
  415.     public class Masukkan extends AsyncTask<String, String, String>
  416.     {
  417.         String success;
  418.  
  419.         @Override
  420.         protected void onPreExecute() {
  421.             super.onPreExecute();
  422.             pDialog = new ProgressDialog(MainActivity.this);
  423.             pDialog.setMessage("Loading ...");
  424.             pDialog.setIndeterminate(false);
  425.             pDialog.show();
  426.         }
  427.  
  428.         @Override
  429.         protected String doInBackground(String... arg0) {
  430.             String strNama = inputNama.getText().toString();
  431.  
  432.             List<NameValuePair> params = new ArrayList<NameValuePair>();
  433.             params.add(new BasicNameValuePair("nama", strNama));
  434.  
  435.             JSONObject json = jParser.makeHttpRequest(urls, "POST", params);
  436.  
  437.             try {
  438.                 success = json.getString("success");
  439.  
  440.             } catch (Exception e) {
  441.                 Toast.makeText(getApplicationContext(), "Error",
  442.                         Toast.LENGTH_LONG).show();
  443.             }
  444.             return null;
  445.         }
  446.         protected void onPostExecute(String file_url) {
  447.             // dismiss the dialog once done
  448.             pDialog.dismiss();
  449.  
  450.             if (success.equals(1))
  451.             {
  452.                 Toast.makeText(getApplicationContext(), "Sukses Input Data", Toast.LENGTH_LONG).show();
  453.             }
  454.             else
  455.             {
  456.                 Toast.makeText(getApplicationContext(), "Gagal Submit Data", Toast.LENGTH_LONG).show();
  457.  
  458.             }
  459.         }
  460.     }
  461. }
Advertisement
Add Comment
Please, Sign In to add comment