How to select by the bank the value of the column using the Random() method

Asked

Viewed 66 times

0

I made a project on Android that is a little hangman game, where I can record in the Sqlite database the word and a hint. In my Databasehelper class I created the normal database, and made some selects of the column where the words are stored and sent as Arraylist to a class where I randomized those words. How can I associate tips with randomized 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();
    }

}

1 answer

0


Good morning,

In the Tip() method you can pass the word and use Where in sql to search in the database.

public String Tip(String palavra){
        String dadosDica = new String();
        SQLiteDatabase db = getReadableDatabase();
        String selectQuery = "SELECT dica FROM palavrasforca WHERE palavra=?";
        Cursor cursor = db.rawQuery(selectQuery, new String[] { palavra });

        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
    }

If you want to click in memory you can use Map to associate,

Map works as a Dictionary, you store the key and the value

Map<String, String> mapPalavras = new HashMap<String, String>();
        mapaNomes.put("Palavra1", "Dica Palavra 1");
        mapaNomes.put("Palavra2", "Dica Palavra 2");
        mapaNomes.put("Palavra3", "Dica Palavra 3");


String dica = mapPalavras.get("Palavra1") //retorna "Dica Palavra 1"

just at the time of calling Word(), you take the word and the hint and store in a Map as the example..

Browser other questions tagged

You are not signed in. Login or sign up in order to post.