lundi 6 juillet 2015

how to add the image data into the SQLite android?

I have a database of quiz, the question in the form of a string. I want to change the question into an image. how to add a question in the form of images into the database? how to make a picture as a question?

public class DBHelper extends SQLiteOpenHelper {

private static final int DATABASE_VERSION = 1;

private static final String DATABASE_NAME = "Tebak Gambar";

private static final String TABLE_HEWAN = "hewan";

//colum soal hewan
private static final String ID = "id";
private static final String QUESTION2 = "quest2";
private static final String JAWABAN = "jawaban";

private SQLiteDatabase dbase;

public DBHelper(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    dbase=db;
    String Create_Database = "CREATE TABLE IF NOT EXISTS " + TABLE_HEWAN + " ( "
            + ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + QUESTION2
            + " TEXT, " + JAWABAN + " TEXT)";
    db.execSQL(Create_Database);
    addQuestions();
}

public void addQuestions(Question2 question2) {
    ContentValues values = new ContentValues();
    values.put(QUESTION2, question2.getQUESTION2());
    values.put(JAWABAN, question2.getJAWABAN());
    dbase.insert(TABLE_HEWAN, null, values);
}

public void  addQuestions(){
    Question2 q1 = new Question2("Gambar Apakah ini?", "awak");
    this.addQuestions(q1);
    Question2 q2 = new Question2("Kapan saya akan ...", "nikah");
    this.addQuestions(q2);
    Question2 q3 = new Question2("menanti sebuah..", "jawaban");
    this.addQuestions(q3);
    Question2 q4 = new Question2("Apakan nama hewan ini?", "gajah");
    this.addQuestions(q4);
    Question2 q5 = new Question2("Hewan hidup di air?", "ikan");
    this.addQuestions(q5);
    Question2 q6 = new Question2("Hewan pemakan daging?", "harimau");
    this.addQuestions(q6);
    Question2 q7 = new Question2("Hewan bisa terbang?", "burung");
    this.addQuestions(q7);
    Question2 q8 = new Question2("Hewan yang larinya cepat", "kuda");
    this.addQuestions(q8);
    Question2 q9 = new Question2("Hewan yang bisa hidup di air dan darat", "buaya");
    this.addQuestions(q9);
    Question2 q10 = new Question2("Hewan melata?", "ular");
    this.addQuestions(q10);

}

public List<Question2> getAllQuestions2() {
    List<Question2> que2List = new ArrayList<Question2>();
    String select_Query = "SELECT * FROM " + TABLE_HEWAN;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(select_Query, null);

    if (cursor.moveToFirst()) {
        do {
            Question2 quest2 = new Question2();
            quest2.setID(cursor.getInt(0));
            quest2.setQUESTION2(cursor.getString(1));
            quest2.setJAWABAN(cursor.getString(2));
            que2List.add(quest2);
        } while (cursor.moveToNext());
    }
    return que2List;
}
public int rowCount(){
    int row = 0;
    String select_Query = "SELECT * FROM " + TABLE_HEWAN;
    dbase = this.getReadableDatabase();
    Cursor cursor = dbase.rawQuery(select_Query, null);
    row = cursor.getCount();
    return row;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_HEWAN);
    onCreate(db);
}

}

public class Quis extends Activity {

int qid2 = 0;
int score=0;

List<Question2> questList2;
Question2 question2;
private TextView textQuestion;
private EditText tJawaban;
private Button bNext;

public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.soal2);

    DBHelper db = new DBHelper(this);
    questList2 = db.getAllQuestions2();

    Collections.shuffle(questList2);

    question2 = questList2.get(qid2);

    textQuestion = (TextView) findViewById(R.id.textQuestion);
    tJawaban = (EditText) findViewById(R.id.editJawaban);
    bNext = (Button) findViewById(R.id.BtnNextquiz2);

    setQuestionsView();

    bNext.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final String jawaban = tJawaban.getText().toString();
            Log.d("jawaban", question2.getJAWABAN() + " " + jawaban);

            if (jawaban.length() == 0){
                tJawaban.requestFocus();
                tJawaban.setError("Jawaban Tidak Boleh Kosong");
            } if (question2.getJAWABAN().equals(jawaban)){
                tJawaban.setText("");
                // get your custom_toast.xml ayout
                LayoutInflater inflater = getLayoutInflater();

                View layout = inflater.inflate(R.layout.custom_toast,
                        (ViewGroup) findViewById(R.id.custom_toast_layout_id));

                // set a dummy image
                ImageView image = (ImageView) layout.findViewById(R.id.image);
                image.setImageResource(R.drawable.benar);

                // Toast...
                Toast toast = new Toast(getApplicationContext());
                toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                toast.setDuration(Toast.LENGTH_LONG);
                toast.setView(layout);
                toast.show();
                score++;
                Log.d("Score", "Your Score :" + score);
            } if (qid2 < 10 ){
                question2 = questList2.get(qid2);
                setQuestionsView();
            } else {
                Intent i = new Intent(Quis.this, ResultActivity.class);
                Bundle b = new Bundle();
                b.putInt("score", score);
                i.putExtras(b);
                startActivity(i);
                finish();
            }
        }
    });

}

public void setQuestionsView(){
    textQuestion.setText(question2.getQUESTION2());
    qid2++;
}

}

Aucun commentaire:

Enregistrer un commentaire