SHARE
TWEET

Soal

Ahmada21 Feb 16th, 2020 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.example.myapplication;
  2.  
  3.  
  4. import android.content.Intent;
  5. import android.graphics.drawable.Drawable;
  6. import android.os.Bundle;
  7. import android.view.LayoutInflater;
  8. import android.view.View;
  9. import android.view.ViewGroup;
  10. import android.widget.Button;
  11. import android.widget.ImageView;
  12. import android.widget.LinearLayout;
  13. import android.widget.TextView;
  14. import android.widget.Toast;
  15.  
  16. import androidx.fragment.app.Fragment;
  17.  
  18.  
  19. /**
  20.  * A simple {@link Fragment} subclass.
  21.  */
  22. public class ToolsFragment extends Fragment {
  23.     private QuestionBank mQuestionLibrary = new QuestionBank();
  24.     private TextView mScoreView;   // Untuk menampilkan skor saat ini
  25.     private TextView mQuestionView;  //menampilkan pertanyaan
  26.     private ImageView mImageView;// menampilkan gambar
  27.     private TextView mQuestionView2;  //menampilkan pertanyaan
  28.     private Button mButtonChoice1; // jawaban 1
  29.     private Button mButtonChoice2; // jawaban 2
  30.     private Button mButtonChoice3; //jawaban 3
  31.     private Button mButtonChoice4; //jawaban 4
  32.     private Button mButtonChoice5; //jawaban 5
  33.  
  34.     private String mAnswer;  // untuk cek jawaban benar atau tidaknya
  35.     private int mScore = 0;  // current total score
  36.     private int mQuestionNumber = 0; //
  37.  
  38.  
  39.     @Override
  40.     public View onCreateView(LayoutInflater inflater, ViewGroup container,
  41.                              Bundle savedInstanceState) {
  42.         View v = inflater.inflate(R.layout.fragment_tools, container, false);
  43.         mScoreView = (TextView)v.findViewById(R.id.score);
  44.         mQuestionView = (TextView)v.findViewById(R.id.question);
  45.         mImageView = (ImageView)v.findViewById(R.id.gambar);
  46.         mQuestionView2 = (TextView)v.findViewById(R.id.question2);
  47.         mButtonChoice1 = (Button)v.findViewById(R.id.choice1);
  48.         mButtonChoice2 = (Button)v.findViewById(R.id.choice2);
  49.         mButtonChoice3 = (Button)v.findViewById(R.id.choice3);
  50.         mButtonChoice4 = (Button)v.findViewById(R.id.choice4);
  51.         mButtonChoice5 = (Button)v.findViewById(R.id.choice5);
  52.         LinearLayout layout = (LinearLayout) v.findViewById(R.id.fragment_tools);
  53.  
  54.         layout.setOnClickListener(new View.OnClickListener() {
  55.             @Override
  56.             public void onClick(View v) {
  57.                 if (   == mAnswer){
  58.                     mScore = mScore + 10;
  59.                     Toast.makeText(ToolsFragment.this.getActivity(), "Benar!", Toast.LENGTH_SHORT).show();
  60.                 }else
  61.                     Toast.makeText(ToolsFragment.this.getActivity(), "Salah!", Toast.LENGTH_SHORT).show();
  62.  
  63.                 updateScore(mScore);
  64.                 updateQuestion();
  65.             }
  66.         });
  67.         updateQuestion();
  68.         updateScore(mScore);
  69.  
  70.         return v;
  71.  
  72.     }
  73.  
  74.  
  75.     private void updateQuestion(){
  76.         //
  77.         if(mQuestionNumber<mQuestionLibrary.getLength() ){
  78.  
  79.  
  80.             mQuestionView.setText(mQuestionLibrary.getQuestion(mQuestionNumber));
  81.             int imageResource = getResources().getIdentifier("@drawable/"+mQuestionLibrary.getPictQuestion(mQuestionNumber),null,getActivity().getPackageName());
  82.             Drawable res = getResources().getDrawable(imageResource);
  83.             mImageView.setImageDrawable(res);
  84.             mQuestionView2.setText(mQuestionLibrary.getQuestion2(mQuestionNumber));
  85.             mButtonChoice1.setText(mQuestionLibrary.getChoice(mQuestionNumber, 1));
  86.             mButtonChoice2.setText(mQuestionLibrary.getChoice(mQuestionNumber, 2));
  87.             mButtonChoice3.setText(mQuestionLibrary.getChoice(mQuestionNumber, 3));
  88.             mButtonChoice4.setText(mQuestionLibrary.getChoice(mQuestionNumber,4));
  89.             mButtonChoice5.setText(mQuestionLibrary.getChoice(mQuestionNumber,5));
  90.             mAnswer = mQuestionLibrary.getCorrectAnswer(mQuestionNumber);
  91.             mQuestionNumber++;
  92.         }
  93.         else {
  94.             Toast.makeText(getActivity(), "It was the last question!", Toast.LENGTH_SHORT).show();
  95.             Intent intent = new Intent(getContext(), HighscoreFragment.class);
  96.             intent.putExtra("score", mScore); // pass the current score to the second screen
  97.             getContext().startActivity(intent);
  98.             getActivity().finish();
  99.         }
  100.     }
  101.  
  102.     private void updateScore(int point) {
  103.         mScoreView.setText("" + mScore+"/"+mQuestionLibrary.getLength() * 10);
  104.     }
  105.  
  106.     public void pilihan(View view) {
  107.         Button answer = (Button) view;
  108.         if (answer.getText() == mAnswer){
  109.             mScore = mScore + 10;
  110.             Toast.makeText(this.getActivity(), "Benar!", Toast.LENGTH_SHORT).show();
  111.         }else
  112.             Toast.makeText(this.getActivity(), "Salah!", Toast.LENGTH_SHORT).show();
  113.  
  114.         updateScore(mScore);
  115.         updateQuestion();
  116.     }
  117.  
  118. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top