1

我正在尝试构建一个 Android 应用程序,但我正在努力完成测验模块。所以在这个模块中,我需要为问题(简单、中等和困难)插入难度设置。对于我的第一个 XML 布局,我将方法设置为返回难度设置。

/**
 * Method to return the difficulty settings
 * @return
 */
private int getDifficultySettings() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
    return diff;

对于另一个显示难度设置的布局。

/**
 * Method to update default check box
 */
private void updateButtonWithPreferences() {
    RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
    RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
    RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);

    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);

    switch (diff)
    {
    case Constants.EASY : 
        c1.toggle();
        break;

    case Constants.MEDIUM : 
        c2.toggle();
        break;

    case Constants.HARD :
        c3.toggle();
        break;  
    }
}

问题是当我选择中等设置时,我只能从数据库中检索问题。各位Android专家能给我一些建议吗?谢谢你。:)

对于困难的java代码: -

public class SettingsActivity extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    Button updateBtn = (Button) findViewById(R.id.nextBtn);
    updateBtn.setOnClickListener(this);

    updateButtonWithPreferences();

}

private void updateButtonWithPreferences() {
    RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
    RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
    RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);

    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);

    switch (diff)
    {
    case Constants.EASY : 
        c1.toggle();
        break;

    case Constants.MEDIUM : 
        c2.toggle();
        break;

    case Constants.EXTREME :
        c3.toggle();
        break;  
    }
}

@Override
public void onClick(View arg0) {

    if (!checkSelected())
    {
        return;
    }
    else
    {
        SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
        Editor e = settings.edit();
        e.putInt(Constants.DIFFICULTY, getSelectedSetting());
        e.commit();
        finish();
    }

}

private boolean checkSelected() {
    RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
    RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
    RadioButton c3 = (RadioButton)findViewById(R.id.hardSetting);
    return (c1.isChecked() || c2.isChecked() || c3.isChecked());
}

private int getSelectedSetting() {
    RadioButton c1 = (RadioButton)findViewById(R.id.easySetting);
    RadioButton c2 = (RadioButton)findViewById(R.id.mediumSetting);
    if (c1.isChecked())
    {
        return Constants.EASY;
    }
    if (c2.isChecked())
    {
        return Constants.MEDIUM;
    }

    return Constants.EXTREME;
}

}

对于数据库 java 代码:-

public class DBHelper extends SQLiteOpenHelper{

private static String DB_PATH = "/data/data/com.tmm.android.chuck/databases/";
private static String DB_NAME = "questionsDb";
private SQLiteDatabase myDataBase; 
private final Context myContext;

public DBHelper(Context context) {
    super(context, DB_NAME, null, 1);
    this.myContext = context;
}   

public void createDataBase() throws IOException{
    boolean dbExist = checkDataBase();
    if(!dbExist)
    {
        this.getReadableDatabase();

        try {
            copyDataBase(); 
        } catch (IOException e) {
            throw new Error("Error copying database");
        }
    }
}

private boolean checkDataBase(){
    SQLiteDatabase checkDB = null;
    try{
        String myPath = DB_PATH + DB_NAME;
        checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
    }catch(SQLiteException e){

    }
    if(checkDB != null){
        checkDB.close();
    }

    return checkDB != null ? true : false;
}

private void copyDataBase() throws IOException{

    InputStream myInput = myContext.getAssets().open(DB_NAME);

    String outFileName = DB_PATH + DB_NAME;

    OutputStream myOutput = new FileOutputStream(outFileName);

    byte[] buffer = new byte[1024];
    int length;
    while ((length = myInput.read(buffer))>0){
        myOutput.write(buffer, 0, length);
    }

    myOutput.flush();
    myOutput.close();
    myInput.close();

}

public void openDataBase() throws SQLException{
    String myPath = DB_PATH + DB_NAME;
    myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
}

@Override
public synchronized void close() {
    if(myDataBase != null)
        myDataBase.close();
    super.close();
}

@Override
public void onCreate(SQLiteDatabase db) {
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}

public List<Question> getQuestionSet(int difficulty, int numQ){
    List<Question> questionSet = new ArrayList<Question>();
    Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE DIFFICULTY=" + difficulty +
            " ORDER BY RANDOM() LIMIT " + numQ, null);
    while (c.moveToNext()){
        Question q = new Question();
        q.setQuestion(c.getString(1));
        q.setAnswer(c.getString(2));
        q.setOption1(c.getString(3));
        q.setOption2(c.getString(4));
        q.setOption3(c.getString(5));
        q.setRating(difficulty);
        questionSet.add(q);
    }
    return questionSet;
}

}

此代码用于我的第一个 XML 布局。所以方法 getQuestionSet() 来自这里。

public class SplashActivity extends Activity implements OnClickListener{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.welcome);

    Button playBtn = (Button) findViewById(R.id.playBtn);
    playBtn.setOnClickListener(this);
    Button settingsBtn = (Button) findViewById(R.id.settingsBtn);
    settingsBtn.setOnClickListener(this);
    Button rulesBtn = (Button) findViewById(R.id.rulesBtn);
    rulesBtn.setOnClickListener(this);
    Button exitBtn = (Button) findViewById(R.id.exitBtn);
    exitBtn.setOnClickListener(this);
}


@Override
public void onClick(View v) {
    Intent i;

    switch (v.getId()){
    case R.id.playBtn :

        List<Question> questions = getQuestionSetFromDb();

        GamePlay c = new GamePlay();
        c.setQuestions(questions);
        c.setNumRounds(getNumQuestions());
        ((ChuckApplication)getApplication()).setCurrentGame(c);  

        i = new Intent(this, QuestionActivity.class);
        startActivityForResult(i, Constants.PLAYBUTTON);
        break;

    case R.id.rulesBtn :
        i = new Intent(this, RulesActivity.class);
        startActivityForResult(i, Constants.RULESBUTTON);
        break;

    case R.id.settingsBtn :
        i = new Intent(this, SettingsActivity.class);
        startActivityForResult(i, Constants.SETTINGSBUTTON);
        break;

    case R.id.exitBtn :
        finish();
        break;
    }
}

private List<Question> getQuestionSetFromDb() throws Error {
    int diff = getDifficultySettings();
    int numQuestions = getNumQuestions();
    DBHelper myDbHelper = new DBHelper(this);
    try {
        myDbHelper.createDataBase();
    } catch (IOException ioe) {
        throw new Error("Unable to create database");
    }
    try {
        myDbHelper.openDataBase();
    }catch(SQLException sqle){
        throw sqle;
    }
    List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions);
    myDbHelper.close();
    return questions;
}

private int getDifficultySettings() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int diff = settings.getInt(Constants.DIFFICULTY, Constants.MEDIUM);
    return diff;
}

private int getNumQuestions() {
    SharedPreferences settings = getSharedPreferences(Constants.SETTINGS, 0);
    int numRounds = settings.getInt(Constants.NUM_ROUNDS, 20);
    return numRounds;
}

}

4

0 回答 0