我必须为一个大学项目制作一个 Hangman 应用程序,目前我已经创建了一个 SQLite 数据库并在其中输入了几个词。我正在尝试运行查询以选择一个随机单词并将其显示在文本视图中,但我无法让结果显示。
这是我到目前为止所拥有的
import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.TextView;
class Hangman extends Activity {
public TextView wordText;
public Button button02A;
private Button button01B;
private Button button03C;
private Button button04D;
private Button button05E;
private Button button06F;
private Button button07G;
private Button button1H;
private Button button09I;
private Button button10J;
private Button button11K;
private Button button12L;
private Button button13M;
private Button button14N;
private Button button15O;
private Button button16P;
private Button button18Q;
private Button button19R;
private Button button20S;
private Button button21T;
private Button button17U;
private Button button22V;
private Button button23W;
private Button button08X;
private Button button25Y;
private Button button26Z;
private Button button24Voice;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hangman);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_hangman, menu);
return true;
}
public void assignButtons() {
wordText = (TextView) findViewById(R.id.wordText);
button02A = (Button) findViewById(R.id.Button02);
button01B = (Button) findViewById(R.id.Button01);
button03C = (Button) findViewById(R.id.Button03);
button04D = (Button) findViewById(R.id.Button04);
button05E = (Button) findViewById(R.id.Button05);
button06F = (Button) findViewById(R.id.Button06);
button07G = (Button) findViewById(R.id.Button07);
button1H = (Button) findViewById(R.id.button1);
button09I = (Button) findViewById(R.id.Button09);
button10J = (Button) findViewById(R.id.Button10);
button11K = (Button) findViewById(R.id.Button11);
button12L = (Button) findViewById(R.id.Button12);
button13M = (Button) findViewById(R.id.Button13);
button14N = (Button) findViewById(R.id.Button14);
button15O = (Button) findViewById(R.id.Button15);
button16P = (Button) findViewById(R.id.Button16);
button18Q = (Button) findViewById(R.id.Button18);
button19R = (Button) findViewById(R.id.Button19);
button20S = (Button) findViewById(R.id.Button20);
button21T = (Button) findViewById(R.id.Button21);
button17U = (Button) findViewById(R.id.Button17);
button22V = (Button) findViewById(R.id.Button22);
button23W = (Button) findViewById(R.id.Button23);
button08X = (Button) findViewById(R.id.Button08);
button25Y = (Button) findViewById(R.id.Button25);
button26Z = (Button) findViewById(R.id.Button26);
button24Voice = (Button) findViewById(R.id.Button24);
}
}
class NewDatabase extends SQLiteOpenHelper {
//Create Database
private static final String DATABASE_NAME = "myDatabase.db";
private static final String DATABASE_TABLE ="Puzzles";
public static final String COLUMN_PUZZLES = "Puzzlescol";
public static final String COLUMN_ID = "_id";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table "
+ DATABASE_TABLE + "(" + COLUMN_ID
+ " integer primary key autoincrement, " + COLUMN_PUZZLES
+ " text not null);";
public NewDatabase(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
//Overriding onCreate and onUpgrade to create database
@Override
public void onCreate(SQLiteDatabase database) {
database.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(NewDatabase.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
public void insertRows(){
ContentValues values = new ContentValues();
values.put(COLUMN_ID, "1");
values.put(COLUMN_PUZZLES, "MERMAID");
values.put(COLUMN_ID, "2");
values.put(COLUMN_PUZZLES, "SUITCASE");
values.put(COLUMN_ID, "3");
values.put(COLUMN_PUZZLES, "OSTRICH");
}
public String randWord() {
SQLiteDatabase db = this.getReadableDatabase();
Cursor cur = db.query("DATABASE_TABLE Order BY RANDOM() LIMIT 1",
new String[] { "*" }, null, null, null, null, null);
return cur.getString(cur.getColumnIndex(COLUMN_PUZZLES));
}
public void showWord(Cursor cur) {
wordText.setText(cur);
}
}