Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

package com.edugame.

dzikrul;

import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RatingBar;
import android.widget.TextView;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

/**
*
*/
public class PertanyaanKelas1 extends Activity {
private static final String DB_NAME = "edugame";
private SQLiteDatabase database;

HashMap<Integer, String> jawaban = new HashMap<>();

String id_pertanyaan;
String pertanyaan;
int id_jawaban;
int noPertanyaan = 1;

int varA = 15;


int varC = 11;
int varM = 37;

int jumlahSoalYangTampil = 10;


int jumlahJawabanBenar;
int jumlahJawabanSalah;

ImageView iv;
Bundle b;
RatingBar bar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.tampilsoal);
bar = (RatingBar) findViewById(R.id.ratingBar);
iv = (ImageView) findViewById(R.id.gambar);
b = getIntent().getExtras();
ambilData();

public void ambilData() {


// ambil dan buka data dari database sqlite
DatabaseHelper dbOpenHelper = new DatabaseHelper(this,
DB_NAME);
database = dbOpenHelper.openDataBase();
// select semua data dari database
Cursor c = database.rawQuery("SELECT * FROM soal_kelas1 ORDER BY RANDOM()
LIMIT 1",
null);
//Cursor c = database.rawQuery("select * from soal_kelas1 WHERE id >=
(abs(random())%(SELECT max (id) from soal_kelas1)) limit 1",
//null);
// simpan data ke variabel lokal untuk digunakan selanjutnya
if (c.moveToFirst()) {
do {
id_pertanyaan = c.getString(0);
pertanyaan = c.getString(1);
id_jawaban = c.getInt(2);
} while (c.moveToNext());
}

// ambil data jawaban


String[] param = new String[1];
param[0] = id_pertanyaan;
c = database.rawQuery("SELECT * FROM jawaban_kelas1 WHERE id_soal = ?" ,
param);
if (c.moveToFirst()) {
do {
jawaban.put(c.getInt(0), c.getString(1));
} while (c.moveToNext());
}
// ambil jumlah total pertanyaan
c = database.rawQuery("select COUNT(id) AS jumlah from soal_kelas1",
null);
if (c.moveToFirst()) {
do {
// varM = c.getInt(0);;
} while (c.moveToNext());
}

c.close();

database.close();
tampilkandata();

TextView barPertanyaan = (TextView) findViewById(R.id.barPertanyaan);


barPertanyaan.setText("Pertanyaan ke-" + noPertanyaan + " dari " +
jumlahSoalYangTampil + " soal");
noPertanyaan++;
}
// menampilkan soal, gambar dan jawaban
public void tampilkandata() {

TextView tv = (TextView) findViewById(R.id.soal);


tv.setText(pertanyaan);

try {
InputStream ims = null;
ims = getAssets().open("kelas1/" + id_pertanyaan.toLowerCase() + ".png");
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
iv.setImageDrawable(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// return;
}
RadioGroup group = (RadioGroup) findViewById(R.id.jawaban);
group.clearCheck();
group.removeAllViews();

for(Map.Entry<Integer, String> entry : jawaban.entrySet()) {


RadioButton rdbtn = new RadioButton(this);
rdbtn.setId(entry.getKey());
rdbtn.setText(entry.getValue());
group.addView(rdbtn);
}

public void okeJawab(View view) {


RadioGroup group = (RadioGroup) findViewById(R.id.jawaban);
TextView pesan = (TextView) findViewById(R.id.pesan);
if(group.getCheckedRadioButtonId() != -1) {
pesan.setText("");
fungsiJawab();
}
else {
pesan.setText("Silahkan pilih salah satu jawaban");
}
}

// fungsi tombol jawab


public void fungsiJawab(){

RadioGroup group = (RadioGroup) findViewById(R.id.jawaban);


int jawabanTerpilih = group.getCheckedRadioButtonId();
if(jawabanTerpilih == id_jawaban){
jumlahJawabanBenar++;
} else {
jumlahJawabanSalah++;
}

TextView barPertanyaan = (TextView) findViewById(R.id.barPertanyaan);

if(jumlahSoalYangTampil < noPertanyaan){


barPertanyaan.setText("Selesai . . .");

TextView tv = (TextView) findViewById(R.id.soal);


tv.setText("Jawaban kamu yang benar adalah " + jumlahJawabanBenar + " dari
" + jumlahSoalYangTampil + " pertanyaan");

Button tj = (Button) findViewById(R.id.tombolJawab);


ViewGroup layout = (ViewGroup) tj.getParent();
layout.removeView(tj);

group.clearCheck();
group.removeAllViews();
//iv.setVisibility(View.INVISIBLE);

rating();

TextView score = (TextView) findViewById(R.id.score);


int Score = jumlahJawabanBenar*10;
score.setText("Score Kamu Adalah " + Score);

if (6 <= jumlahJawabanBenar){
try {
InputStream ims = null;
ims = getAssets().open("emo/smile.png");
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
iv.setImageDrawable(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// return;
}
}
else {
try {
InputStream ims = null;
ims = getAssets().open("emo/sad.png");
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
iv.setImageDrawable(d);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
// return;
}
}

} else {

int diPertanyaanSub = Integer.parseInt(id_pertanyaan);


Integer idNextPertanyaan = randomMetode(diPertanyaanSub);
if(0 == idNextPertanyaan){
idNextPertanyaan = 1;
}
id_pertanyaan = idNextPertanyaan.toString();

String[] param = new String[1];


param[0] = id_pertanyaan;

// ambil dan buka data dari database sqlite


DatabaseHelper dbOpenHelper = new DatabaseHelper(this,
DB_NAME);
database = dbOpenHelper.openDataBase();
// select pertanyaan dan jawaban
Cursor c = database.rawQuery("select soal, id_jawaban from soal_kelas1
WHERE id=?",
param);
// simpan data ke variabel lokal untuk digunakan selanjutnya
if (c.moveToFirst()) {
do {
pertanyaan = c.getString(0);
id_jawaban = c.getInt(1);
} while (c.moveToNext());
}
// ambil data jawaban
c = database.rawQuery("SELECT * FROM jawaban_kelas1 WHERE id_soal = ?" ,
param);
jawaban.clear();
if (c.moveToFirst()) {
do {
jawaban.put(c.getInt(0), c.getString(1));
} while (c.moveToNext());
}

c.close();
database.close();

tampilkandata();
barPertanyaan.setText("Pertanyaan ke-" + noPertanyaan + " dari " +
jumlahSoalYangTampil + " soal");

noPertanyaan++;
}
}

// random metode utama


public int randomMetode(int xn) {
int xn1 = ((varA * xn) + varC) % varM;
return xn1;
}

//ratingBar
private void rating() {
//ambil obyek ratingBar
bar.setNumStars(5);
float nilaiBar = jumlahJawabanBenar / 2;
bar.setRating(nilaiBar);
}

You might also like