I made a project in Android that is a little game of the gallows, where I can write in the SQLite database the word and a tip. In my DatabaseHelper class I created the normal database, and made some selects from the column where the words are stored and sent as Arraylist to a class where I randomize those words. How can I link the tips to random words?
DatabaseHelper class, where I create the bank
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import java.util.ArrayList;
import java.util.List;
import dominio.android.forca.data.DataBaseDescription.Contact;
class AddressBookDatabaseHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "Forca.db";
private static final int DATABASE_VERSION = 1;
// Criamos aqui uma classe chamada DatabaseHelper, que serve para
// manipular a abertura e manutenção do banco de dados em si.
public AddressBookDatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// creates the contacts table when the database is created
@Override
public void onCreate(SQLiteDatabase db) {
// SQL for creating the contacts table
final String CREATE_CONTACTS_TABLE =
"CREATE TABLE " + Contact.TABLE_NAME + "(" +
Contact._ID + " integer primary key, " +
Contact.COLUMN_WORD + " TEXT, " +
Contact.COLUMN_TIP + " TEXT);";
db.execSQL(CREATE_CONTACTS_TABLE); // create the contacts table
}
// normally defines how to upgrade the database when the schema changes
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
// MÉTODO FEITO POR UM LIST DE STRING
public List<String> Word(){
List<String> dadosPalavra = new ArrayList();
SQLiteDatabase db = getReadableDatabase();
String selectQuery = "SELECT palavra FROM palavrasforca";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do{
String palavra = cursor.getString(cursor.getColumnIndex("palavra"));
dadosPalavra.add(palavra);
}while (cursor.moveToNext());
}
return dadosPalavra;
//aqui dados terá todos os valores do banco
}
// MÉTODO FEITO POR UM LIST DE STRING
public String Tip(){
String dadosDica = new String();
SQLiteDatabase db = getReadableDatabase();
String selectQuery = "SELECT dica FROM palavrasforca";
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do{
String palavra = cursor.getString(cursor.getColumnIndex("dica"));
dadosDica = palavra;
}while (cursor.moveToNext());
}
return dadosDica;
//aqui dados terá todos os valores do banco
}
}
And the class where I make the random choice of words:
import android.content.Context;
import java.util.List;
import java.util.Random;
public class Palavras {
AddressBookDatabaseHelper dbHelper;
public Palavras(Context context) {
dbHelper = new AddressBookDatabaseHelper(context.getApplicationContext());
}
public Palavras() { }
public String sorteio() {
List<String> lista_palavras = dbHelper.Word();
String palavraSorteada = lista_palavras.get((int)(random()*lista_palavras.size()));
return palavraSorteada;
}
public String dica(){
String tipSorteada = dbHelper.Tip();
String dicaSorteada = tipSorteada;
return dicaSorteada;
}
public static double random() {
Random r = new Random();
return r.nextDouble();
}
}